diff --git a/app/Config/Logger.php b/app/Config/Logger.php index 64e4aa1929a2..93d38f6f638d 100644 --- a/app/Config/Logger.php +++ b/app/Config/Logger.php @@ -34,15 +34,6 @@ class Logger extends BaseConfig */ public $threshold = 3; - /* - |-------------------------------------------------------------------------- - | Error Logging Directory Path - |-------------------------------------------------------------------------- - | By default, logs are written to WRITEPATH . 'logs/' - | Specify a different destination here, if desired. - */ - public $path = ''; - /* |-------------------------------------------------------------------------- | Date Format for Logs @@ -116,6 +107,14 @@ class Logger extends BaseConfig * integer notation (i.e. 0700, 0644, etc.) */ 'filePermissions' => 0644, + + /* + * Logging Directory Path + * + * By default, logs are written to WRITEPATH . 'logs/' + * Specify a different destination here, if desired. + */ + 'path' => '', ], /** diff --git a/system/Log/Handlers/FileHandler.php b/system/Log/Handlers/FileHandler.php index 78a321862728..41df096660ed 100644 --- a/system/Log/Handlers/FileHandler.php +++ b/system/Log/Handlers/FileHandler.php @@ -77,7 +77,7 @@ public function __construct(array $config = []) { parent::__construct($config); - $this->path = $config['path'] ?? WRITEPATH . 'logs/'; + $this->path = empty($config['path']) ? WRITEPATH . 'logs/' : $config['path']; $this->fileExtension = empty($config['fileExtension']) ? 'log' : $config['fileExtension']; $this->fileExtension = ltrim($this->fileExtension, '.'); diff --git a/system/Test/Mock/MockLogger.php b/system/Test/Mock/MockLogger.php index 837c3661beeb..239dab59177e 100644 --- a/system/Test/Mock/MockLogger.php +++ b/system/Test/Mock/MockLogger.php @@ -33,16 +33,6 @@ class MockLogger public $threshold = 9; - /* - |-------------------------------------------------------------------------- - | Error Logging Directory Path - |-------------------------------------------------------------------------- - | - | - | - */ - public $path = ''; - /* |-------------------------------------------------------------------------- | Date Format for Logs @@ -97,6 +87,11 @@ class MockLogger 'notice', 'warning', ], + + /* + * Logging Directory Path + */ + 'path' => '', ], ]; diff --git a/tests/system/Log/FileHandlerTest.php b/tests/system/Log/FileHandlerTest.php index 68b03a4362c5..dbea88b9dc47 100644 --- a/tests/system/Log/FileHandlerTest.php +++ b/tests/system/Log/FileHandlerTest.php @@ -30,25 +30,28 @@ public function testHandle() public function testBasicHandle() { $config = new LoggerConfig(); - $config->path = $this->start . 'charlie/'; + $config->handlers['Tests\Support\Log\Handlers\TestHandler']['path'] = $this->start . 'charlie/'; $config->handlers['Tests\Support\Log\Handlers\TestHandler']['handles'] = ['critical']; $logger = new MockFileLogger($config->handlers['Tests\Support\Log\Handlers\TestHandler']); $logger->setDateFormat('Y-m-d H:i:s:u'); + $expected = 'log-' . date('Y-m-d') . '.log'; + vfsStream::newFile($expected)->at(vfsStream::setup('root/charlie'))->withContent('This is a test log'); $this->assertTrue($logger->handle('warning', 'This is a test log')); } public function testHandleCreateFile() { - $config = new LoggerConfig(); - $config->path = $this->start; - $logger = new MockFileLogger((array) $config); + $config = new LoggerConfig(); + $config->handlers['Tests\Support\Log\Handlers\TestHandler']['path'] = $this->start; + $logger = new MockFileLogger($config->handlers['Tests\Support\Log\Handlers\TestHandler']); $logger->setDateFormat('Y-m-d H:i:s:u'); + $expected = 'log-' . date('Y-m-d') . '.log'; + vfsStream::newFile($expected)->at(vfsStream::setup('root'))->withContent('This is a test log'); $logger->handle('warning', 'This is a test log'); - $expected = 'log-' . date('Y-m-d') . '.log'; - $fp = fopen($config->path . $expected, 'r'); - $line = fgets($fp); + $fp = fopen($config->handlers['Tests\Support\Log\Handlers\TestHandler']['path'] . $expected, 'r'); + $line = fgets($fp); fclose($fp); // did the log file get created? @@ -58,15 +61,15 @@ public function testHandleCreateFile() public function testHandleDateTimeCorrectly() { - $config = new LoggerConfig(); - $config->path = $this->start; - $logger = new MockFileLogger((array) $config); + $config = new LoggerConfig(); + $config->handlers['Tests\Support\Log\Handlers\TestHandler']['path'] = $this->start; + $logger = new MockFileLogger($config->handlers['Tests\Support\Log\Handlers\TestHandler']); $logger->setDateFormat('Y-m-d'); $expected = 'log-' . date('Y-m-d') . '.log'; - + vfsStream::newFile($expected)->at(vfsStream::setup('root'))->withContent('Test message'); $logger->handle('debug', 'Test message'); - $fp = fopen($config->path . $expected, 'r'); + $fp = fopen($config->handlers['Tests\Support\Log\Handlers\TestHandler']['path'] . $expected, 'r'); $line = fgets($fp); // and get the second line fclose($fp);