From 153f04a976493145768a1c29415d35626a10d6ec Mon Sep 17 00:00:00 2001 From: Gareth Ellis Date: Fri, 22 Apr 2016 09:18:38 +0100 Subject: [PATCH] Changes behaviour of _useLogger so that already-configured loggers are not overridden --- lib/Cake/Console/Shell.php | 33 ++++++++++++++++++++++++ lib/Cake/Test/Case/Console/ShellTest.php | 27 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/lib/Cake/Console/Shell.php b/lib/Cake/Console/Shell.php index ff4237529..506046e55 100644 --- a/lib/Cake/Console/Shell.php +++ b/lib/Cake/Console/Shell.php @@ -964,15 +964,48 @@ class Shell extends Object { 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); + } } diff --git a/lib/Cake/Test/Case/Console/ShellTest.php b/lib/Cake/Test/Case/Console/ShellTest.php index 38c7a08f9..4c9f443ff 100644 --- a/lib/Cake/Test/Case/Console/ShellTest.php +++ b/lib/Cake/Test/Case/Console/ShellTest.php @@ -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,26 @@ TEXT; public function testGetInvalidHelper() { $this->Shell->helper("tomato"); } + + /** + * @return void + */ + public function testShellLoggersDoNotGetOverridden() + { + $shell = $this->getMock("Shell", array( + "_loggerIsConfigured", + "configureStdOutLogger", + "configureStdErrLogger", + )); + + $shell->expects($this->any()) + ->method("_loggerIsConfigured") + ->will($this->returnValue(true)); + + $shell->expects($this->never()) + ->method("_configureStdOutLogger"); + + $shell->expects($this->never()) + ->method("_configureStdErrLogger"); + } }