| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Monolog package. |
| 5 |
* |
| 6 |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Monolog log registry |
| 14 |
* |
| 15 |
* Allows to get `Logger` instances in the global scope |
| 16 |
* via static method calls on this class. |
| 17 |
* |
| 18 |
* <code> |
| 19 |
* $application = new Monolog\Logger('application'); |
| 20 |
* $api = new Monolog\Logger('api'); |
| 21 |
* |
| 22 |
* Monolog\Registry::addLogger($application); |
| 23 |
* Monolog\Registry::addLogger($api); |
| 24 |
* |
| 25 |
* function testLogger() |
| 26 |
* { |
| 27 |
* Monolog\Registry::api()->addError('Sent to $api Logger instance'); |
| 28 |
* Monolog\Registry::application()->addError('Sent to $application Logger instance'); |
| 29 |
* } |
| 30 |
* </code> |
| 31 |
* |
| 32 |
* @author Tomas Tatarko <tomas@tatarko.sk> |
| 33 |
*/ |
| 34 |
class Monolog_Registry |
| 35 |
{ |
| 36 |
/** |
| 37 |
* List of all loggers in the registry (ba named indexes) |
| 38 |
* |
| 39 |
* @var Monolog_Logger[] |
| 40 |
*/ |
| 41 |
private static $loggers = array(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Adds new logging channel to the registry |
| 45 |
* |
| 46 |
* @param Monolog_Logger $logger Instance of the logging channel |
| 47 |
* @param string|null $name Name of the logging channel ($logger->getName() by default) |
| 48 |
* @param boolean $overwrite Overwrite instance in the registry if the given name already exists? |
| 49 |
* |
| 50 |
* @throws InvalidArgumentException If $overwrite set to false and named Logger instance already exists |
| 51 |
*/ |
| 52 |
public static function addLogger(Monolog_Logger $logger, $name = null, $overwrite = false) |
| 53 |
{ |
| 54 |
$name = $name ? $name : $logger->getName(); |
| 55 |
|
| 56 |
if (isset(self::$loggers[$name]) && !$overwrite) { |
| 57 |
throw new InvalidArgumentException('Logger with the given name already exists'); |
| 58 |
} |
| 59 |
|
| 60 |
self::$loggers[$name] = $logger; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Removes instance from registry by name or instance |
| 65 |
* |
| 66 |
* @param string|Monolog_Logger $logger Name or logger instance |
| 67 |
*/ |
| 68 |
public static function removeLogger($logger) |
| 69 |
{ |
| 70 |
if ($logger instanceof Monolog_Logger) { |
| 71 |
if (false !== ($idx = array_search($logger, self::$loggers, true))) { |
| 72 |
unset(self::$loggers[$idx]); |
| 73 |
} |
| 74 |
} else { |
| 75 |
unset(self::$loggers[$logger]); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Clears the registry |
| 81 |
*/ |
| 82 |
public static function clear() |
| 83 |
{ |
| 84 |
self::$loggers = array(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Gets Logger instance from the registry |
| 89 |
* |
| 90 |
* @param string $name Name of the requested Logger instance |
| 91 |
* |
| 92 |
* @return Monolog_Logger Requested instance of Logger |
| 93 |
* @throws InvalidArgumentException If named Logger instance is not in the registry |
| 94 |
*/ |
| 95 |
public static function getInstance($name) |
| 96 |
{ |
| 97 |
if (!isset(self::$loggers[$name])) { |
| 98 |
throw new InvalidArgumentException(sprintf('Requested "%s" logger instance is not in the registry', $name)); |
| 99 |
} |
| 100 |
|
| 101 |
return self::$loggers[$name]; |
| 102 |
} |
| 103 |
} |
| 104 |
|