mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-05 12:47:39 +02:00
Moved all the logic to the separated class - Monolog\Registry
This commit is contained in:
@@ -107,12 +107,6 @@ class Logger implements LoggerInterface
|
||||
*/
|
||||
protected static $timezone;
|
||||
|
||||
/**
|
||||
* List of all active Logger instances
|
||||
* @var array of Monolog\Logger
|
||||
*/
|
||||
protected static $loggerInstances = array();
|
||||
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
@@ -131,43 +125,6 @@ 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters Logger instance
|
||||
* @param string $name The logging channel name
|
||||
*/
|
||||
public static function unregisterLogger($name)
|
||||
{
|
||||
if(isset(self::$loggerInstances[$name])) {
|
||||
self::$loggerInstances[$name] = null;
|
||||
unset(self::$loggerInstances[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, $arguments)
|
||||
{
|
||||
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.
|
||||
@@ -178,7 +135,6 @@ class Logger implements LoggerInterface
|
||||
$this->name = $name;
|
||||
$this->handlers = $handlers;
|
||||
$this->processors = $processors;
|
||||
self::registerLogger($this->name, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -620,4 +576,12 @@ class Logger implements LoggerInterface
|
||||
{
|
||||
return $this->addRecord(static::EMERGENCY, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds instance to the Registry
|
||||
*/
|
||||
public function addToRegistry()
|
||||
{
|
||||
Registry::addLogger($this);
|
||||
}
|
||||
}
|
||||
|
121
src/Monolog/Registry.php
Normal file
121
src/Monolog/Registry.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Monolog log registry
|
||||
*
|
||||
* Allows to get `Logger` instances in the global scope
|
||||
* via static method calls on this class.
|
||||
*
|
||||
* <code>
|
||||
* $application = new Monolog\Logger('application');
|
||||
* $api = new Monolog\Logger('api');
|
||||
*
|
||||
* $application->addToRegistry();
|
||||
* Monolog\Registry::addLogger($api);
|
||||
*
|
||||
* function testLogger()
|
||||
* {
|
||||
* Monolog\Registry::api()->addError('Sent to $api Logger instance');
|
||||
* Monolog\Registry::application()->addError('Sent to $application Logger instance');
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @author Tomas Tatarko <tomas@tatarko.sk>
|
||||
*/
|
||||
class Registry
|
||||
{
|
||||
/**
|
||||
* List of all loggers in the registry (ba named indexes)
|
||||
* @var array of Monolog\Logger
|
||||
*/
|
||||
protected static $loggers = array();
|
||||
|
||||
/**
|
||||
* Adds new logging channel to the registry
|
||||
* @param Monolog\Logger $logger Instance of the logging channel
|
||||
* @param string $name Name of the logging channel ($logger->getName() by default)
|
||||
* @param boolean $overwrite Overwrite instance in the registry if the given name already exists?
|
||||
* @throws InvalidArgumentException If $overwrite set to false and named Logger instance already exists
|
||||
*/
|
||||
public static function addLogger(Logger $logger, $name = null, $overwrite = false)
|
||||
{
|
||||
$name = $name ? : $logger->getName();
|
||||
|
||||
if (isset(self::$loggers[$name]) && !$overwrite) {
|
||||
throw new InvalidArgumentException('Logger with the given name already exists');
|
||||
}
|
||||
|
||||
self::$loggers[$name] = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes instance from registry by the given name
|
||||
* @param string $name Named index to remove
|
||||
*/
|
||||
public static function removeLoggerByName($name)
|
||||
{
|
||||
self::$loggers[$name] = null;
|
||||
unset(self::$loggers[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes instance from registry by the given instance
|
||||
* @param Monolog\Logger $instance Instance thats pointer should be removed from the registry
|
||||
*/
|
||||
public static function removeLoggerByInstance(Logger $instance)
|
||||
{
|
||||
foreach (self::$loggers as $key => $logger) {
|
||||
if ($logger === $instance) {
|
||||
self::removeLoggerByName($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the registry
|
||||
*/
|
||||
public static function clear()
|
||||
{
|
||||
self::$loggers = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Logger instance from the registry
|
||||
* @param string $name Name of the requested Logger instance
|
||||
* @return Monolog\Logger Requested instance of Logger
|
||||
* @throws InvalidArgumentException If named Logger instance is not in the registry
|
||||
*/
|
||||
public static function getInstance($name)
|
||||
{
|
||||
if (!isset(self::$loggers[$name])) {
|
||||
throw new InvalidArgumentException(sprintf('Requested "%s" logger instance is not in the registry', $name));
|
||||
}
|
||||
|
||||
return self::$loggers[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Logger instance from the registry via static method call
|
||||
* @param string $name Name of the requested Logger instance
|
||||
* @param array $arguments Arguments passed to static method call
|
||||
* @return Monolog\Logger Requested instance of Logger
|
||||
* @throws InvalidArgumentException If named Logger instance is not in the registry
|
||||
*/
|
||||
public static function __callStatic($name, $arguments)
|
||||
{
|
||||
return self::getInstance($name);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user