MediaWiki master
LoggerFactory.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Logger;
8
9use LogicException;
10use Wikimedia\ObjectFactory\ObjectFactory;
11
33
38 private static $spi;
39
40 private static ?LoggingContext $context;
41
48 public static function registerProvider( Spi $provider ) {
49 self::$spi = $provider;
50 }
51
64 public static function getProvider() {
65 if ( self::$spi === null ) {
67 $provider = ObjectFactory::getObjectFromSpec(
69 );
70 self::registerProvider( $provider );
71 }
72 return self::$spi;
73 }
74
81 public static function getInstance( $channel ) {
82 return self::getProvider()->getLogger( $channel );
83 }
84
89 public static function getContext(): LoggingContext {
90 self::$context ??= new LoggingContext();
91 return self::$context;
92 }
93
99 public static function setContext( LoggingContext $context ): void {
100 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
101 throw new LogicException( __METHOD__ . ' can only be used in PHPUnit tests' );
102 }
103 self::$context = $context;
104 }
105
109 private function __construct() {
110 // no-op
111 }
112}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
Create PSR-3 logger objects.
static registerProvider(Spi $provider)
Register a service provider to create new \Psr\Log\LoggerInterface instances.
static getInstance( $channel)
Get a named logger instance from the currently configured logger factory.
static setContext(LoggingContext $context)
Replace the logging context, for testing.
static getContext()
Get a logging context, which can be used to add information to all log events.
static getProvider()
Get the registered service provider.
A helper class for adding extra context to all logs, without the logging code having to be aware.
$wgMWLoggerDefaultSpi
Config variable stub for the MWLoggerDefaultSpi setting, for use by phpdoc and IDEs.
Service provider interface to create \Psr\Log\LoggerInterface objects.
Definition Spi.php:50
Helper trait for implementations \DAO.