1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\Log; |
3
|
|
|
|
4
|
|
|
use Psr\Log\AbstractLogger; |
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Psr\Log\InvalidArgumentException; |
7
|
|
|
use Psr\Log\LogLevel; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* LoggerManager is a PSR-3 logger that can delegate |
11
|
|
|
* log messages to other loggers. This is ideal if |
12
|
|
|
* you need to inject a logger into various objects |
13
|
|
|
* in your application, but need to change the way that |
14
|
|
|
* the application logs later. |
15
|
|
|
* |
16
|
|
|
* @author Greg Anderson <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class LoggerManager extends AbstractLogger implements StylableLoggerInterface |
19
|
|
|
{ |
20
|
|
|
/** @var LoggerInterface[] */ |
21
|
|
|
protected $loggers = []; |
22
|
|
|
/** @var LoggerInterface */ |
23
|
|
|
protected $fallbackLogger = null; |
24
|
|
|
/** @var LogOutputStylerInterface */ |
25
|
|
|
protected $outputStyler; |
26
|
|
|
/** @var array */ |
27
|
|
|
protected $formatFunctionMap = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* reset removes all loggers from the manager. |
31
|
|
|
*/ |
32
|
|
|
public function reset() |
33
|
|
|
{ |
34
|
|
|
$this->loggers = []; |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* setLogOutputStyler will remember a style that |
40
|
|
|
* should be applied to every stylable logger |
41
|
|
|
* added to this manager. |
42
|
|
|
*/ |
43
|
|
|
public function setLogOutputStyler(LogOutputStylerInterface $outputStyler, array $formatFunctionMap = array()) |
44
|
|
|
{ |
45
|
|
|
$this->outputStyler = $outputStyler; |
46
|
|
|
$this->formatFunctionMap = $this->formatFunctionMap; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* add adds a named logger to the manager, |
51
|
|
|
* replacing any logger of the same name. |
52
|
|
|
* |
53
|
|
|
* @param string $name Name of logger to add |
54
|
|
|
* @param LoggerInterface $logger Logger to send messages to |
55
|
|
|
*/ |
56
|
|
|
public function add($name, LoggerInterface $logger) |
57
|
|
|
{ |
58
|
|
|
// If this manager has been given a log style, |
59
|
|
|
// and the logger being added accepts a log |
60
|
|
|
// style, then copy our style to the logger |
61
|
|
|
// being added. |
62
|
|
|
if ($this->outputStyler && $logger instanceof StylableLoggerInterface) { |
63
|
|
|
$logger->setLogOutputStyler($this->outputStyler, $this->formatFunctionMap); |
64
|
|
|
} |
65
|
|
|
$this->loggers[$name] = $logger; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* remove a named logger from the manager. |
71
|
|
|
* |
72
|
|
|
* @param string $name Name of the logger to remove. |
73
|
|
|
*/ |
74
|
|
|
public function remove($name) |
75
|
|
|
{ |
76
|
|
|
unset($this->loggers[$name]); |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* fallbackLogger provides a logger that will |
82
|
|
|
* be used only in instances where someone logs |
83
|
|
|
* to the logger manager at a time when there |
84
|
|
|
* are no other loggers registered. If there is |
85
|
|
|
* no fallback logger, then the log messages |
86
|
|
|
* are simply dropped. |
87
|
|
|
* |
88
|
|
|
* @param LoggerInterface $logger Logger to use as the fallback logger |
89
|
|
|
*/ |
90
|
|
|
public function fallbackLogger(LoggerInterface $logger) |
91
|
|
|
{ |
92
|
|
|
$this->fallbackLogger = $logger; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function log($level, $message, array $context = array()) |
100
|
|
|
{ |
101
|
|
|
foreach ($this->getLoggers() as $logger) { |
102
|
|
|
$logger->log($level, $message, $context); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return either the list of registered loggers, |
108
|
|
|
* or a single-element list containing only the |
109
|
|
|
* fallback logger. |
110
|
|
|
*/ |
111
|
|
|
protected function getLoggers() |
112
|
|
|
{ |
113
|
|
|
if (!empty($this->loggers)) { |
114
|
|
|
return $this->loggers; |
115
|
|
|
} |
116
|
|
|
if (isset($this->fallbackLogger)) { |
117
|
|
|
return [ $this->fallbackLogger ]; |
118
|
|
|
} |
119
|
|
|
return []; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|