From 6779cf1e04a675412418f82df6d561a4179b86a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Tatarko?= Date: Thu, 14 Nov 2013 14:44:19 +0100 Subject: [PATCH] Moved all the logic to the separated class - Monolog\Registry --- src/Monolog/Logger.php | 52 +++-------------- src/Monolog/Registry.php | 121 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 44 deletions(-) create mode 100644 src/Monolog/Registry.php diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index d3af1024..bc172893 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -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); + } } diff --git a/src/Monolog/Registry.php b/src/Monolog/Registry.php new file mode 100644 index 00000000..d20a1914 --- /dev/null +++ b/src/Monolog/Registry.php @@ -0,0 +1,121 @@ + + * + * 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. + * + * + * $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'); + * } + * + * + * @author Tomas Tatarko + */ +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); + } +} \ No newline at end of file