LoggerFactory.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. namespace MediaWiki\Logger;
  21. use Wikimedia\ObjectFactory;
  22. /**
  23. * PSR-3 logger instance factory.
  24. *
  25. * Creation of \Psr\Log\LoggerInterface instances is managed via the
  26. * LoggerFactory::getInstance() static method which in turn delegates to the
  27. * currently registered service provider.
  28. *
  29. * A service provider is any class implementing the Spi interface.
  30. * There are two possible methods of registering a service provider. The
  31. * LoggerFactory::registerProvider() static method can be called at any time
  32. * to change the service provider. If LoggerFactory::getInstance() is called
  33. * before any service provider has been registered, it will attempt to use the
  34. * $wgMWLoggerDefaultSpi global to bootstrap Spi registration.
  35. * $wgMWLoggerDefaultSpi is expected to be an array usable by
  36. * ObjectFactory::getObjectFromSpec() to create a class.
  37. *
  38. * @see \MediaWiki\Logger\Spi
  39. * @since 1.25
  40. * @copyright © 2014 Wikimedia Foundation and contributors
  41. */
  42. class LoggerFactory {
  43. /**
  44. * Service provider.
  45. * @var \MediaWiki\Logger\Spi $spi
  46. */
  47. private static $spi;
  48. /**
  49. * Register a service provider to create new \Psr\Log\LoggerInterface
  50. * instances.
  51. *
  52. * @param \MediaWiki\Logger\Spi $provider Provider to register
  53. */
  54. public static function registerProvider( Spi $provider ) {
  55. self::$spi = $provider;
  56. }
  57. /**
  58. * Get the registered service provider.
  59. *
  60. * If called before any service provider has been registered, it will
  61. * attempt to use the $wgMWLoggerDefaultSpi global to bootstrap
  62. * Spi registration. $wgMWLoggerDefaultSpi is expected to be an
  63. * array usable by ObjectFactory::getObjectFromSpec() to create a class.
  64. *
  65. * @return \MediaWiki\Logger\Spi
  66. * @see registerProvider()
  67. * @see ObjectFactory::getObjectFromSpec()
  68. */
  69. public static function getProvider() {
  70. if ( self::$spi === null ) {
  71. global $wgMWLoggerDefaultSpi;
  72. $provider = ObjectFactory::getObjectFromSpec(
  73. $wgMWLoggerDefaultSpi
  74. );
  75. self::registerProvider( $provider );
  76. }
  77. return self::$spi;
  78. }
  79. /**
  80. * Get a named logger instance from the currently configured logger factory.
  81. *
  82. * @param string $channel Logger channel (name)
  83. * @return \Psr\Log\LoggerInterface
  84. */
  85. public static function getInstance( $channel ) {
  86. return self::getProvider()->getLogger( $channel );
  87. }
  88. /**
  89. * Construction of utility class is not allowed.
  90. */
  91. private function __construct() {
  92. // no-op
  93. }
  94. }