blob: a3eba0790a85e7a788ba19f7838c91103d78154d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
<?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');
*
* Monolog\Registry::addLogger($application);
* 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 Logger[]
*/
private static $loggers = array();
/**
* Adds new logging channel to the registry
*
* @param Logger $logger Instance of the logging channel
* @param string|null $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 name or instance
*
* @param string|Logger $logger Name or logger instance
*/
public static function removeLogger($logger)
{
if ($logger instanceof Logger) {
if (false !== ($idx = array_search($logger, self::$loggers, true))) {
unset(self::$loggers[$idx]);
}
} else {
unset(self::$loggers[$logger]);
}
}
/**
* 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 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 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);
}
}
|