1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-09 06:36:46 +02:00

Ability to access logger instances in the global scope via static calls

This commit is contained in:
Tomáš Tatarko
2013-11-13 21:13:08 +01:00
parent ad859c3dba
commit a9b57679b3

View File

@@ -107,6 +107,12 @@ class Logger implements LoggerInterface
*/
protected static $timezone;
/**
* List of all active Logger instances
* @var array of Monolog\Logger
*/
protected static $loggerInstances = array();
protected $name;
/**
@@ -125,6 +131,31 @@ class Logger implements LoggerInterface
*/
protected $processors;
/**
* Registers new Logger instance
* @param string $name The logging channel name
* @param Monolog\Logger $instance Instance to register
*/
public static function registerLogger($name, Logger $instance)
{
self::$loggerInstances[$name] = $instance;
}
/**
* Gets instance of requested logging channel
* @param string $name The logging channel name
* @return Monolog\Logger
* @throws InvalidArgumentException If logging channel has not been created
*/
public static function __callStatic($name)
{
if(!isset(self::$loggerInstances[$name])) {
throw new \InvalidArgumentException('The requested logging channel has not been created yet');
}
return self::$loggerInstances[$name];
}
/**
* @param string $name The logging channel
* @param array $handlers Optional stack of handlers, the first one in the array is called first, etc.
@@ -135,6 +166,7 @@ class Logger implements LoggerInterface
$this->name = $name;
$this->handlers = $handlers;
$this->processors = $processors;
self::registerLogger($this->name, $this);
}
/**