Skip to content
Permalink
Browse files

Vary the logging levels based on verbosity parameters.

Control logging levels through the --quiet and --verbose flags. This
lets console logging to be controlled with the same options as output
levels.

Refs #7417
  • Loading branch information...
markstory committed Sep 23, 2015
1 parent 4bc0f2d commit e4e20876352a3479931cb192ee69a2f517fb4629
Showing with 69 additions and 16 deletions.
  1. +16 −7 src/Console/ConsoleIo.php
  2. +23 −9 src/Console/Shell.php
  3. +30 −0 tests/TestCase/Console/ConsoleIoTest.php
@@ -353,21 +353,30 @@ protected function _getInput($prompt, $options, $default)
* If you don't wish all log output in stdout or stderr
* through Cake's Log class, call this function with `$enable=false`.
*
* @param bool $enable Whether you want loggers on or off.
* @param int|bool $enable Use a boolean to enable/toggle all logging. Use
* one of the verbosity constants (self::VERBOSE, self::QUIET, self::NORMAL)
* to control logging levels. VERBOSE enables debug logs, NORMAL does not include debug logs,
* QUIET disables notice, info and debug logs.
* @return void
*/
public function setLoggers($enable)
{
Log::drop('stdout');
Log::drop('stderr');
if (!$enable) {
if ($enable === false) {
return;
}
$stdout = new ConsoleLog([
'types' => ['notice', 'info', 'debug'],
'stream' => $this->_out
]);
Log::config('stdout', ['engine' => $stdout]);
$outLevels = ['notice', 'info'];
if ($enable === static::VERBOSE || $enable === true) {
$outLevels[] = 'debug';
}
if ($enable !== static::QUIET) {
$stdout = new ConsoleLog([
'types' => $outLevels,
'stream' => $this->_out
]);
Log::config('stdout', ['engine' => $stdout]);
}
$stderr = new ConsoleLog([
'types' => ['emergency', 'alert', 'critical', 'error', 'warning'],
'stream' => $this->_err,
@@ -163,11 +163,11 @@ public function __construct(ConsoleIo $io = null)
$this->modelFactory('Table', [$locator, 'get']);
$this->Tasks = new TaskRegistry($this);
$this->_io->setLoggers(true);
$this->_mergeVars(
['tasks'],
['associative' => ['tasks']]
);
$this->_io->setLoggers(true);
if (isset($this->modelClass)) {
$this->loadModel();
@@ -410,14 +410,7 @@ public function runCommand($argv, $autoMethod = false, $extra = [])
if (!empty($extra) && is_array($extra)) {
$this->params = array_merge($this->params, $extra);
}
if (!empty($this->params['quiet'])) {
$this->_io->level(ConsoleIo::QUIET);
$this->_io->setLoggers(false);
}
if (!empty($this->params['verbose'])) {
$this->_io->level(ConsoleIo::VERBOSE);
}
$this->_setOutputLevel();
if (!empty($this->params['plugin'])) {
Plugin::load($this->params['plugin']);
}
@@ -456,6 +449,27 @@ public function runCommand($argv, $autoMethod = false, $extra = [])
return false;
}
/**
* Set the output level based on the parameters.
*
* This reconfigures both the output level for out()
* and the configured stdout/stderr logging
*
* @return void
*/
protected function _setOutputLevel()
{
$this->_io->setLoggers(ConsoleIo::NORMAL);
if (!empty($this->params['quiet'])) {
$this->_io->level(ConsoleIo::QUIET);
$this->_io->setLoggers(ConsoleIo::QUIET);
}
if (!empty($this->params['verbose'])) {
$this->_io->level(ConsoleIo::VERBOSE);
$this->_io->setLoggers(ConsoleIo::VERBOSE);
}
}
/**
* Display the help in the correct format
*
@@ -345,6 +345,36 @@ public function testSetLoggers()
$this->assertFalse(Log::engine('stderr'));
}
/**
* Tests that setLoggers works properly with quiet
*
* @return void
*/
public function testSetLoggersQuiet()
{
Log::drop('stdout');
Log::drop('stderr');
$this->io->setLoggers(ConsoleIo::QUIET);
$this->assertEmpty(Log::engine('stdout'));
$this->assertNotEmpty(Log::engine('stderr'));
}
/**
* Tests that setLoggers works properly with verbose
*
* @return void
*/
public function testSetLoggersVerbose()
{
Log::drop('stdout');
Log::drop('stderr');
$this->io->setLoggers(ConsoleIo::VERBOSE);
$this->assertNotEmpty(Log::engine('stderr'));
$engine = Log::engine('stdout');
$this->assertEquals(['notice', 'info', 'debug'], $engine->config('levels'));
}
/**
* Ensure that styles() just proxies to stdout.
*

0 comments on commit e4e2087

Please sign in to comment.
You can’t perform that action at this time.