|
22 | 22 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
23 | 23 |
|
24 | 24 | /** |
25 | | - * Writes to the console output depending on its verbosity setting. |
| 25 | + * Writes logs to the console output depending on its verbosity setting. |
26 | 26 | * |
27 | 27 | * It is disabled by default and gets activated as soon as a command is executed. |
28 | 28 | * Instead of listening to the console events, setOutput can also be called manually. |
29 | 29 | * |
| 30 | + * The minimum logging level at which this handler will be triggered depends on the |
| 31 | + * verbosity setting of the console output. The default mapping is: |
| 32 | + * - OutputInterface::VERBOSITY_NORMAL will show all WARNING and higher logs |
| 33 | + * - OutputInterface::VERBOSITY_VERBOSE (-v) will show all NOTICE and higher logs |
| 34 | + * - OutputInterface::VERBOSITY_VERY_VERBOSE (-vv) will show all INFO and higher logs |
| 35 | + * - OutputInterface::VERBOSITY_DEBUG (-vvv) will show all DEBUG and higher logs, i.e. all logs |
| 36 | + * |
| 37 | + * This mapping can be customized with the $verbosityLevelMap constructor parameter. |
| 38 | + * |
30 | 39 | * @author Tobias Schultze <http://tobion.de> |
31 | 40 | */ |
32 | 41 | class ConsoleHandler extends AbstractProcessingHandler implements EventSubscriberInterface |
33 | 42 | { |
34 | 43 | /** |
35 | 44 | * @var OutputInterface|null |
36 | 45 | */ |
37 | | - protected $output; |
| 46 | + private $output; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var array |
| 50 | + */ |
| 51 | + private $verbosityLevelMap = array( |
| 52 | + OutputInterface::VERBOSITY_NORMAL => Logger::WARNING, |
| 53 | + OutputInterface::VERBOSITY_VERBOSE => Logger::NOTICE, |
| 54 | + OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO, |
| 55 | + OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG |
| 56 | + ); |
38 | 57 |
|
39 | 58 | /** |
40 | 59 | * Constructor. |
41 | 60 | * |
42 | | - * The minimum logging level it is set based on the output verbosity in setOutput. |
43 | | - * |
44 | | - * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not |
| 61 | + * @param Boolean $bubble Whether the messages that are handled can bubble up the stack |
| 62 | + * @param array $verbosityLevelMap Array that maps the OutputInterface verbosity to a minimum logging level |
| 63 | + * (leave empty to use the default mapping) |
45 | 64 | */ |
46 | | - public function __construct($bubble = true) |
| 65 | + public function __construct($bubble = true, array $verbosityLevelMap = array()) |
47 | 66 | { |
48 | 67 | parent::__construct(Logger::DEBUG, $bubble); |
| 68 | + |
| 69 | + if ($verbosityLevelMap) { |
| 70 | + $this->verbosityLevelMap = $verbosityLevelMap; |
| 71 | + } |
49 | 72 | } |
50 | 73 |
|
51 | 74 | /** |
@@ -145,23 +168,14 @@ protected function getDefaultFormatter() |
145 | 168 | */ |
146 | 169 | private function updateLevel() |
147 | 170 | { |
148 | | - if (null === $this->output || OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) { |
| 171 | + if (null === $this->output || OutputInterface::VERBOSITY_QUIET === $verbosity = $this->output->getVerbosity()) { |
149 | 172 | return false; |
150 | 173 | } |
151 | 174 |
|
152 | | - switch ($this->output->getVerbosity()) { |
153 | | - case OutputInterface::VERBOSITY_NORMAL: |
154 | | - $this->setLevel(Logger::WARNING); |
155 | | - break; |
156 | | - case OutputInterface::VERBOSITY_VERBOSE: |
157 | | - $this->setLevel(Logger::NOTICE); |
158 | | - break; |
159 | | - case OutputInterface::VERBOSITY_VERY_VERBOSE: |
160 | | - $this->setLevel(Logger::INFO); |
161 | | - break; |
162 | | - default: |
163 | | - $this->setLevel(Logger::DEBUG); |
164 | | - break; |
| 175 | + if (isset($this->verbosityLevelMap[$verbosity])) { |
| 176 | + $this->setLevel($this->verbosityLevelMap[$verbosity]); |
| 177 | + } else { |
| 178 | + $this->setLevel(Logger::DEBUG); |
165 | 179 | } |
166 | 180 |
|
167 | 181 | return true; |
|
0 commit comments