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
119
120
121
<?php
namespace Consolidation\Log;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LogLevel;
/**
* LoggerManager is a PSR-3 logger that can delegate
* log messages to other loggers. This is ideal if
* you need to inject a logger into various objects
* in your application, but need to change the way that
* the application logs later.
*
* @author Greg Anderson <greg.1.anderson@greenknowe.org>
*/
class LoggerManager extends AbstractLogger implements StylableLoggerInterface
{
/** @var LoggerInterface[] */
protected $loggers = [];
/** @var LoggerInterface */
protected $fallbackLogger = null;
/** @var LogOutputStylerInterface */
protected $outputStyler;
/** @var array */
protected $formatFunctionMap = [];
/**
* reset removes all loggers from the manager.
*/
public function reset()
{
$this->loggers = [];
return $this;
}
/**
* setLogOutputStyler will remember a style that
* should be applied to every stylable logger
* added to this manager.
*/
public function setLogOutputStyler(LogOutputStylerInterface $outputStyler, array $formatFunctionMap = array())
{
$this->outputStyler = $outputStyler;
$this->formatFunctionMap = $this->formatFunctionMap;
}
/**
* add adds a named logger to the manager,
* replacing any logger of the same name.
*
* @param string $name Name of logger to add
* @param LoggerInterface $logger Logger to send messages to
*/
public function add($name, LoggerInterface $logger)
{
// If this manager has been given a log style,
// and the logger being added accepts a log
// style, then copy our style to the logger
// being added.
if ($this->outputStyler && $logger instanceof StylableLoggerInterface) {
$logger->setLogOutputStyler($this->outputStyler, $this->formatFunctionMap);
}
$this->loggers[$name] = $logger;
return $this;
}
/**
* remove a named logger from the manager.
*
* @param string $name Name of the logger to remove.
*/
public function remove($name)
{
unset($this->loggers[$name]);
return $this;
}
/**
* fallbackLogger provides a logger that will
* be used only in instances where someone logs
* to the logger manager at a time when there
* are no other loggers registered. If there is
* no fallback logger, then the log messages
* are simply dropped.
*
* @param LoggerInterface $logger Logger to use as the fallback logger
*/
public function fallbackLogger(LoggerInterface $logger)
{
$this->fallbackLogger = $logger;
return $this;
}
/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = array())
{
foreach ($this->getLoggers() as $logger) {
$logger->log($level, $message, $context);
}
}
/**
* Return either the list of registered loggers,
* or a single-element list containing only the
* fallback logger.
*/
protected function getLoggers()
{
if (!empty($this->loggers)) {
return $this->loggers;
}
if (isset($this->fallbackLogger)) {
return [ $this->fallbackLogger ];
}
return [];
}
}