Passed
Push — 4 ( a3d929...a9d57f )
by Guy
07:35
created

MonologErrorHandler::pushLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Logging;
4
5
use InvalidArgumentException;
6
use Psr\Log\LoggerInterface;
7
use Monolog\ErrorHandler as MonologHandler;
8
use SilverStripe\Dev\Deprecation;
9
10
class MonologErrorHandler implements ErrorHandler
11
{
12
    /**
13
     * @var LoggerInterface[]
14
     */
15
    private $loggers = [];
16
17
    /**
18
     * Set the PSR-3 logger to send errors & exceptions to. Will overwrite any previously configured
19
     * loggers
20
     *
21
     * @deprecated 4.4.0:5.0.0 Use pushHandler() instead
22
     * @param LoggerInterface $logger
23
     * @return $this
24
     */
25
    public function setLogger(LoggerInterface $logger)
26
    {
27
        Deprecation::notice('4.4.0', 'Please use pushHandler() instead');
28
29
        $this->loggers = [$logger];
30
        return $this;
31
    }
32
33
    /**
34
     * Get the first registered PSR-3 logger to send errors & exceptions to
35
     *
36
     * @deprecated 4.4.0:5.0.0 Use getHandlers() instead
37
     * @return LoggerInterface
38
     */
39
    public function getLogger()
40
    {
41
        Deprecation::notice('4.4.0', 'Please use getHandlers() instead');
42
43
        return reset($this->loggers);
44
    }
45
46
    /**
47
     * Adds a PSR-3 logger to send messages to, to the end of the stack
48
     *
49
     * @param LoggerInterface $logger
50
     * @return $this
51
     */
52
    public function pushLogger(LoggerInterface $logger)
53
    {
54
        $this->loggers[] = $logger;
55
        return $this;
56
    }
57
58
    /**
59
     * Returns the stack of PSR-3 loggers
60
     *
61
     * @return LoggerInterface[]
62
     */
63
    public function getLoggers()
64
    {
65
        return $this->loggers;
66
    }
67
68
    /**
69
     * Set the PSR-3 loggers (overwrites any previously configured values)
70
     *
71
     * @param LoggerInterface[] $loggers
72
     * @return $this
73
     */
74
    public function setLoggers(array $loggers)
75
    {
76
        $this->loggers = $loggers;
77
        return $this;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     *
83
     * @throws InvalidArgumentException
84
     */
85
    public function start()
86
    {
87
        $loggers = $this->getLoggers();
88
        if (empty($loggers)) {
89
            throw new InvalidArgumentException(
90
                "No Logger properties passed to MonologErrorHandler. Is your Injector config correct?"
91
            );
92
        }
93
94
        foreach ($loggers as $logger) {
95
            MonologHandler::register($logger);
96
        }
97
    }
98
}
99