Merge pull request #8692 from garethellis36/dont-override-shell-loggers

Don't override already-configured loggers
This commit is contained in:
Mark Story 2016-04-22 16:05:03 -04:00
commit be35880b2f
2 changed files with 68 additions and 0 deletions

View file

@ -974,15 +974,48 @@ class Shell extends CakeObject {
CakeLog::drop('stderr');
return;
}
if (!$this->_loggerIsConfigured("stdout")) {
$this->_configureStdOutLogger();
}
if (!$this->_loggerIsConfigured("stderr")) {
$this->_configureStdErrLogger();
}
}
/**
* Configure the stdout logger
*
* @return void
*/
protected function _configureStdOutLogger() {
CakeLog::config('stdout', array(
'engine' => 'Console',
'types' => array('notice', 'info'),
'stream' => $this->stdout,
));
}
/**
* Configure the stderr logger
*
* @return void
*/
protected function _configureStdErrLogger() {
CakeLog::config('stderr', array(
'engine' => 'Console',
'types' => array('emergency', 'alert', 'critical', 'error', 'warning', 'debug'),
'stream' => $this->stderr,
));
}
/**
* Checks if the given logger is configured
*
* @param string $logger The name of the logger to check
* @return bool
*/
protected function _loggerIsConfigured($logger) {
$configured = CakeLog::configured();
return in_array($logger, $configured);
}
}

View file

@ -920,6 +920,8 @@ TEXT;
* @return void
*/
public function testFileAndConsoleLogging() {
CakeLog::disable('stdout');
CakeLog::disable('stderr');
// file logging
$this->Shell->log_something();
$this->assertTrue(file_exists(LOGS . 'error.log'));
@ -944,6 +946,9 @@ TEXT;
$this->assertTrue(file_exists(LOGS . 'error.log'));
$contents = file_get_contents(LOGS . 'error.log');
$this->assertContains($this->Shell->testMessage, $contents);
CakeLog::enable('stdout');
CakeLog::enable('stderr');
}
/**
@ -995,4 +1000,34 @@ TEXT;
public function testGetInvalidHelper() {
$this->Shell->helper("tomato");
}
/**
* Test that shell loggers do not get overridden in constructor if already configured
*
* @return void
*/
public function testShellLoggersDoNotGetOverridden() {
$shell = $this->getMock(
"Shell", array(
"_loggerIsConfigured",
"configureStdOutLogger",
"configureStdErrLogger",
),
array(),
"",
false
);
$shell->expects($this->exactly(2))
->method("_loggerIsConfigured")
->will($this->returnValue(true));
$shell->expects($this->never())
->method("_configureStdOutLogger");
$shell->expects($this->never())
->method("_configureStdErrLogger");
$shell->__construct();
}
}