Skip to content

Commit b6538f6

Browse files
committed
made the verbosity to log level configurable
1 parent 7dc11cc commit b6538f6

File tree

3 files changed

+89
-21
lines changed

3 files changed

+89
-21
lines changed

DependencyInjection/Configuration.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,60 @@ public function getConfigTreeBuilder()
122122
->scalarNode('connection_timeout')->end() // socket_handler
123123
->booleanNode('persistent')->end() // socket_handler
124124
->scalarNode('dsn')->end() // raven_handler
125+
->arrayNode('verbosity_levels') // console
126+
->beforeNormalization()
127+
->ifArray()
128+
->then(function ($v) {
129+
$map = array();
130+
$verbosities = array('VERBOSITY_NORMAL', 'VERBOSITY_VERBOSE', 'VERBOSITY_VERY_VERBOSE', 'VERBOSITY_DEBUG');
131+
// allow numeric indexed array with ascendning verbosity and lowercase names of the constants
132+
foreach ($v as $verbosity => $level) {
133+
if (is_int($verbosity) && isset($verbosities[$verbosity])) {
134+
$map[$verbosities[$verbosity]] = strtoupper($level);
135+
} else {
136+
$map[strtoupper($verbosity)] = strtoupper($level);
137+
}
138+
}
139+
140+
return $map;
141+
})
142+
->end()
143+
->children()
144+
->scalarNode('VERBOSITY_NORMAL')->defaultValue('WARNING')->end()
145+
->scalarNode('VERBOSITY_VERBOSE')->defaultValue('NOTICE')->end()
146+
->scalarNode('VERBOSITY_VERY_VERBOSE')->defaultValue('INFO')->end()
147+
->scalarNode('VERBOSITY_DEBUG')->defaultValue('DEBUG')->end()
148+
->end()
149+
->validate()
150+
->always(function ($v) {
151+
$map = array();
152+
foreach ($v as $verbosity => $level) {
153+
$verbosityConstant = 'Symfony\Component\Console\Output\OutputInterface::'.$verbosity;
154+
155+
if (!defined($verbosityConstant)) {
156+
throw new InvalidConfigurationException(sprintf(
157+
'The configured verbosity "%s" is invalid as it is not defined in Symfony\Component\Console\Output\OutputInterface.',
158+
$verbosity
159+
));
160+
}
161+
if (!is_int($level)) {
162+
$levelConstant = 'Monolog\Logger::'.$level;
163+
if (!defined($levelConstant)) {
164+
throw new InvalidConfigurationException(sprintf(
165+
'The configured minimum log level "%s" for verbosity "%s" is invalid as it is not defined in Monolog\Logger.',
166+
$level, $verbosity
167+
));
168+
}
169+
$level = constant($levelConstant);
170+
}
171+
172+
$map[constant($verbosityConstant)] = $level;
173+
}
174+
175+
return $map;
176+
})
177+
->end()
178+
->end()
125179
->arrayNode('channels')
126180
->fixXmlConfig('channel', 'elements')
127181
->canBeUnset()

DependencyInjection/MonologExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\Config\FileLocator;
1818
use Symfony\Component\DependencyInjection\Definition;
1919
use Symfony\Component\DependencyInjection\Reference;
20-
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2120

2221
/**
2322
* MonologExtension is an extension for the Monolog library.
@@ -126,6 +125,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
126125
case 'console':
127126
$definition->setArguments(array(
128127
$handler['bubble'],
128+
isset($handler['verbosity_levels']) ? $handler['verbosity_levels'] : array()
129129
));
130130
$definition->addTag('kernel.event_subscriber');
131131
break;

Handler/ConsoleHandler.php

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,53 @@
2222
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2323

2424
/**
25-
* Writes to the console output depending on its verbosity setting.
25+
* Writes logs to the console output depending on its verbosity setting.
2626
*
2727
* It is disabled by default and gets activated as soon as a command is executed.
2828
* Instead of listening to the console events, setOutput can also be called manually.
2929
*
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+
*
3039
* @author Tobias Schultze <http://tobion.de>
3140
*/
3241
class ConsoleHandler extends AbstractProcessingHandler implements EventSubscriberInterface
3342
{
3443
/**
3544
* @var OutputInterface|null
3645
*/
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+
);
3857

3958
/**
4059
* Constructor.
4160
*
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)
4564
*/
46-
public function __construct($bubble = true)
65+
public function __construct($bubble = true, array $verbosityLevelMap = array())
4766
{
4867
parent::__construct(Logger::DEBUG, $bubble);
68+
69+
if ($verbosityLevelMap) {
70+
$this->verbosityLevelMap = $verbosityLevelMap;
71+
}
4972
}
5073

5174
/**
@@ -145,23 +168,14 @@ protected function getDefaultFormatter()
145168
*/
146169
private function updateLevel()
147170
{
148-
if (null === $this->output || OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
171+
if (null === $this->output || OutputInterface::VERBOSITY_QUIET === $verbosity = $this->output->getVerbosity()) {
149172
return false;
150173
}
151174

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);
165179
}
166180

167181
return true;

0 commit comments

Comments
 (0)