MediaWiki master
LoggerFactory.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Logger;
22
23use LogicException;
24use Wikimedia\ObjectFactory\ObjectFactory;
25
47
52 private static $spi;
53
54 private static ?LoggingContext $context;
55
62 public static function registerProvider( Spi $provider ) {
63 self::$spi = $provider;
64 }
65
78 public static function getProvider() {
79 if ( self::$spi === null ) {
81 $provider = ObjectFactory::getObjectFromSpec(
83 );
84 self::registerProvider( $provider );
85 }
86 return self::$spi;
87 }
88
95 public static function getInstance( $channel ) {
96 return self::getProvider()->getLogger( $channel );
97 }
98
103 public static function getContext(): LoggingContext {
104 self::$context ??= new LoggingContext();
105 return self::$context;
106 }
107
113 public static function setContext( LoggingContext $context ): void {
114 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
115 throw new LogicException( __METHOD__ . ' can only be used in PHPUnit tests' );
116 }
117 self::$context = $context;
118 }
119
123 private function __construct() {
124 // no-op
125 }
126}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
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:64
A helper class for throttling authentication attempts.