Issues (14)

src/Monolog/ConsoleHandler.php (1 issue)

Labels
Severity
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool\Monolog;
13
14
use Monolog\Formatter\FormatterInterface;
15
use Monolog\Handler\AbstractProcessingHandler;
16
use Monolog\Logger;
17
use Symfony\Component\Console\Output\ConsoleOutputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Writes logs to the console output depending on its verbosity setting.
22
 *
23
 * It is disabled by default and gets activated as soon as a command is executed.
24
 * Instead of listening to the console events, the output can also be set manually.
25
 *
26
 * The minimum logging level at which this handler will be triggered depends on the
27
 * verbosity setting of the console output. The default mapping is:
28
 * - OutputInterface::VERBOSITY_NORMAL will show all WARNING and higher logs
29
 * - OutputInterface::VERBOSITY_VERBOSE (-v) will show all NOTICE and higher logs
30
 * - OutputInterface::VERBOSITY_VERY_VERBOSE (-vv) will show all INFO and higher logs
31
 * - OutputInterface::VERBOSITY_DEBUG (-vvv) will show all DEBUG and higher logs, i.e. all logs
32
 *
33
 * This mapping can be customized with the $verbosityLevelMap constructor parameter.
34
 *
35
 * @author Tobias Schultze <http://tobion.de>
36
 */
37
class ConsoleHandler extends AbstractProcessingHandler
38
{
39
    /**
40
     * @var OutputInterface|null
41
     */
42
    private $output;
43
44
    /**
45
     * @var array
46
     */
47
    private $verbosityLevelMap = [
48
        OutputInterface::VERBOSITY_NORMAL => Logger::WARNING,
49
        OutputInterface::VERBOSITY_VERBOSE => Logger::NOTICE,
50
        OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO,
51
        OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG,
52
    ];
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param OutputInterface|null $output            The console output to use (the handler remains disabled when passing null
58
     *                                                until the output is set, e.g. by using console events)
59
     * @param bool                 $bubble            Whether the messages that are handled can bubble up the stack
60
     * @param array                $verbosityLevelMap Array that maps the OutputInterface verbosity to a minimum logging
61
     *                                                level (leave empty to use the default mapping)
62
     */
63 25
    public function __construct(OutputInterface $output = null, $bubble = true, array $verbosityLevelMap = [])
64
    {
65 25
        parent::__construct(Logger::DEBUG, $bubble);
66 25
        $this->output = $output;
67
68 25
        if (!empty($verbosityLevelMap)) {
69
            $this->verbosityLevelMap = $verbosityLevelMap;
70
        }
71 25
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 23
    public function isHandling(array $record): bool
77
    {
78 23
        return $this->updateLevel() && parent::isHandling($record);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 20
    public function handle(array $record): bool
85
    {
86
        // we have to update the logging level each time because the verbosity of the
87
        // console output might have changed in the meantime (it is not immutable)
88 20
        return $this->updateLevel() && parent::handle($record);
89
    }
90
91
    /**
92
     * Sets the console output to use for printing logs.
93
     *
94
     * @param OutputInterface $output The console output to use
95
     */
96 25
    public function setOutput(OutputInterface $output)
97
    {
98 25
        $this->output = $output;
99 25
    }
100
101
    /**
102
     * Disables the output.
103
     */
104 22
    public function close(): void
105
    {
106 22
        $this->output = null;
107
108 22
        parent::close();
109 22
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 20
    protected function write(array $record): void
115
    {
116 20
        if ($record['level'] >= Logger::ERROR && $this->output instanceof ConsoleOutputInterface) {
117
            $this->output->getErrorOutput()->write((string) $record['formatted']);
118
        } else {
119 20
            $this->output->write((string) $record['formatted']);
0 ignored issues
show
The method write() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
            $this->output->/** @scrutinizer ignore-call */ 
120
                           write((string) $record['formatted']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
        }
121 20
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 20
    protected function getDefaultFormatter(): FormatterInterface
127
    {
128 20
        return new ConsoleFormatter();
129
    }
130
131
    /**
132
     * Updates the logging level based on the verbosity setting of the console output.
133
     *
134
     * @return bool    Whether the handler is enabled and verbosity is not set to quiet.
135
     */
136 23
    private function updateLevel()
137
    {
138 23
        if (null === $this->output || OutputInterface::VERBOSITY_QUIET === $verbosity = $this->output->getVerbosity()) {
139 2
            return false;
140
        }
141
142 21
        if (isset($this->verbosityLevelMap[$verbosity])) {
143 21
            $this->setLevel($this->verbosityLevelMap[$verbosity]);
144
        } else {
145
            $this->setLevel(Logger::DEBUG);
146
        }
147
148 21
        return true;
149
    }
150
}
151