LoggerManager   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 104
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 5 1
A setLogOutputStyler() 0 5 1
A add() 0 12 3
A remove() 0 5 1
A fallbackLogger() 0 5 1
A log() 0 6 2
A getLoggers() 0 10 3
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