Skip to content

Commit

Permalink
Vary the logging levels based on verbosity parameters.
Browse files Browse the repository at this point in the history
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 24, 2015
1 parent 4bc0f2d commit e4e2087
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
23 changes: 16 additions & 7 deletions src/Console/ConsoleIo.php
Expand Up @@ -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,
Expand Down
32 changes: 23 additions & 9 deletions src/Console/Shell.php
Expand Up @@ -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();
Expand Down Expand Up @@ -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']);
}
Expand Down Expand Up @@ -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
*
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/Console/ConsoleIoTest.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit e4e2087

Please sign in to comment.