Completed
Push — 1.x ( 8c31f4...5f4811 )
by Samuel
7s
created

ConsoleHandler::write()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
nc 2
cc 3
eloc 5
nop 1
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\Handler\AbstractProcessingHandler;
15
use Monolog\Logger;
16
use Symfony\Component\Console\Output\ConsoleOutputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Writes logs to the console output depending on its verbosity setting.
21
 *
22
 * It is disabled by default and gets activated as soon as a command is executed.
23
 * Instead of listening to the console events, the output can also be set manually.
24
 *
25
 * The minimum logging level at which this handler will be triggered depends on the
26
 * verbosity setting of the console output. The default mapping is:
27
 * - OutputInterface::VERBOSITY_NORMAL will show all WARNING and higher logs
28
 * - OutputInterface::VERBOSITY_VERBOSE (-v) will show all NOTICE and higher logs
29
 * - OutputInterface::VERBOSITY_VERY_VERBOSE (-vv) will show all INFO and higher logs
30
 * - OutputInterface::VERBOSITY_DEBUG (-vvv) will show all DEBUG and higher logs, i.e. all logs
31
 *
32
 * This mapping can be customized with the $verbosityLevelMap constructor parameter.
33
 *
34
 * @author Tobias Schultze <http://tobion.de>
35
 */
36
class ConsoleHandler extends AbstractProcessingHandler
37
{
38
    /**
39
     * @var OutputInterface|null
40
     */
41
    private $output;
42
43
    /**
44
     * @var array
45
     */
46
    private $verbosityLevelMap = array(
47
        OutputInterface::VERBOSITY_NORMAL => Logger::WARNING,
48
        OutputInterface::VERBOSITY_VERBOSE => Logger::NOTICE,
49
        OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO,
50
        OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG,
51
    );
52
53
    /**
54
     * Constructor.
55
     *
56
     * @param OutputInterface|null $output            The console output to use (the handler remains disabled when passing null
57
     *                                                until the output is set, e.g. by using console events)
58
     * @param bool                 $bubble            Whether the messages that are handled can bubble up the stack
59
     * @param array                $verbosityLevelMap Array that maps the OutputInterface verbosity to a minimum logging
60
     *                                                level (leave empty to use the default mapping)
61
     */
62
    public function __construct(OutputInterface $output = null, $bubble = true, array $verbosityLevelMap = array())
63
    {
64
        parent::__construct(Logger::DEBUG, $bubble);
65
        $this->output = $output;
66
67
        if ($verbosityLevelMap) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $verbosityLevelMap of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
68
            $this->verbosityLevelMap = $verbosityLevelMap;
69
        }
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function isHandling(array $record)
76
    {
77
        return $this->updateLevel() && parent::isHandling($record);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function handle(array $record)
84
    {
85
        // we have to update the logging level each time because the verbosity of the
86
        // console output might have changed in the meantime (it is not immutable)
87
        return $this->updateLevel() && parent::handle($record);
88
    }
89
90
    /**
91
     * Sets the console output to use for printing logs.
92
     *
93
     * @param OutputInterface $output The console output to use
94
     */
95
    public function setOutput(OutputInterface $output)
96
    {
97
        $this->output = $output;
98
    }
99
100
    /**
101
     * Disables the output.
102
     */
103
    public function close()
104
    {
105
        $this->output = null;
106
107
        parent::close();
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    protected function write(array $record)
114
    {
115
        if ($record['level'] >= Logger::ERROR && $this->output instanceof ConsoleOutputInterface) {
116
            $this->output->getErrorOutput()->write((string) $record['formatted']);
117
        } else {
118
            $this->output->write((string) $record['formatted']);
119
        }
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    protected function getDefaultFormatter()
126
    {
127
        return new ConsoleFormatter();
128
    }
129
130
    /**
131
     * Updates the logging level based on the verbosity setting of the console output.
132
     *
133
     * @return bool    Whether the handler is enabled and verbosity is not set to quiet.
134
     */
135
    private function updateLevel()
136
    {
137
        if (null === $this->output || OutputInterface::VERBOSITY_QUIET === $verbosity = $this->output->getVerbosity()) {
138
            return false;
139
        }
140
141
        if (isset($this->verbosityLevelMap[$verbosity])) {
142
            $this->setLevel($this->verbosityLevelMap[$verbosity]);
143
        } else {
144
            $this->setLevel(Logger::DEBUG);
145
        }
146
147
        return true;
148
    }
149
}
150