1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 13:16:39 +02:00

Merge removeLogger* methods in a simpler one, refs #273

This commit is contained in:
Jordi Boggiano
2013-11-25 14:22:15 +01:00
parent c5ea3e6a95
commit 193e627a0b

View File

@@ -39,12 +39,14 @@ class Registry
{ {
/** /**
* List of all loggers in the registry (ba named indexes) * List of all loggers in the registry (ba named indexes)
*
* @var array of Logger * @var array of Logger
*/ */
protected static $loggers = array(); private static $loggers = array();
/** /**
* Adds new logging channel to the registry * Adds new logging channel to the registry
*
* @param Logger $logger Instance of the logging channel * @param Logger $logger Instance of the logging channel
* @param string $name Name of the logging channel ($logger->getName() by default) * @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? * @param boolean $overwrite Overwrite instance in the registry if the given name already exists?
@@ -52,7 +54,7 @@ class Registry
*/ */
public static function addLogger(Logger $logger, $name = null, $overwrite = false) public static function addLogger(Logger $logger, $name = null, $overwrite = false)
{ {
$name = $name ? : $logger->getName(); $name = $name ?: $logger->getName();
if (isset(self::$loggers[$name]) && !$overwrite) { if (isset(self::$loggers[$name]) && !$overwrite) {
throw new InvalidArgumentException('Logger with the given name already exists'); throw new InvalidArgumentException('Logger with the given name already exists');
@@ -62,24 +64,18 @@ class Registry
} }
/** /**
* Removes instance from registry by the given name * Removes instance from registry by name or instance
* @param string $name Named index to remove *
* @param string|Logger $logger Name or logger instance
*/ */
public static function removeLoggerByName($name) public static function removeLogger($logger)
{ {
unset(self::$loggers[$name]); if ($logger instanceof Logger) {
} if (false !== ($idx = array_search($logger, self::$loggers, true))) {
unset(self::$loggers[$idx]);
/**
* Removes instance from registry by the given instance
* @param 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);
} }
} else {
unset(self::$loggers[$logger]);
} }
} }
@@ -93,6 +89,7 @@ class Registry
/** /**
* Gets Logger instance from the registry * Gets Logger instance from the registry
*
* @param string $name Name of the requested Logger instance * @param string $name Name of the requested Logger instance
* @return Logger Requested instance of Logger * @return Logger Requested instance of Logger
* @throws \InvalidArgumentException If named Logger instance is not in the registry * @throws \InvalidArgumentException If named Logger instance is not in the registry
@@ -108,6 +105,7 @@ class Registry
/** /**
* Gets Logger instance from the registry via static method call * Gets Logger instance from the registry via static method call
*
* @param string $name Name of the requested Logger instance * @param string $name Name of the requested Logger instance
* @param array $arguments Arguments passed to static method call * @param array $arguments Arguments passed to static method call
* @return Logger Requested instance of Logger * @return Logger Requested instance of Logger
@@ -117,4 +115,4 @@ class Registry
{ {
return self::getInstance($name); return self::getInstance($name);
} }
} }