Completed
Push — master ( 99acef...f48dd8 )
by Greg
01:25
created

LoggerManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 82
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 5 1
A add() 0 5 1
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
19
{
20
    /** @var LoggerInterface[] */
21
    protected $loggers = [];
22
    /** @var LoggerInterface */
23
    protected $fallbackLogger = null;
24
25
    /**
26
     * reset removes all loggers from the manager.
27
     */
28
    public function reset()
29
    {
30
        $this->loggers = [];
31
        return $this;
32
    }
33
34
    /**
35
     * add adds a named logger to the manager,
36
     * replacing any logger of the same name.
37
     *
38
     * @param string $name Name of logger to add
39
     * @param LoggerInterface $logger Logger to send messages to
40
     */
41
    public function add($name, LoggerInterface $logger)
42
    {
43
        $this->loggers[$name] = $logger;
44
        return $this;
45
    }
46
47
    /**
48
     * remove a named logger from the manager.
49
     *
50
     * @param string $name Name of the logger to remove.
51
     */
52
    public function remove($name)
53
    {
54
        unset($this->loggers[$name]);
55
        return $this;
56
    }
57
58
    /**
59
     * fallbackLogger provides a logger that will
60
     * be used only in instances where someone logs
61
     * to the logger manager at a time when there
62
     * are no other loggers registered. If there is
63
     * no fallback logger, then the log messages
64
     * are simply dropped.
65
     *
66
     * @param LoggerInterface $logger Logger to use as the fallback logger
67
     */
68
    public function fallbackLogger(LoggerInterface $logger)
69
    {
70
        $this->fallbackLogger = $logger;
71
        return $this;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function log($level, $message, array $context = array())
78
    {
79
        foreach ($this->getLoggers() as $logger) {
80
            $logger->log($level, $message, $context);
81
        }
82
    }
83
84
    /**
85
     * Return either the list of registered loggers,
86
     * or a single-element list containing only the
87
     * fallback logger.
88
     */
89
    protected function getLoggers()
90
    {
91
        if (!empty($this->loggers)) {
92
            return $this->loggers;
93
        }
94
        if (isset($this->fallbackLogger)) {
95
            return [ $this->fallbackLogger ];
96
        }
97
        return [];
98
    }
99
}
100