Skip to content

Commit

Permalink
Merge pull request #2726 from samsonasik/fix-2725
Browse files Browse the repository at this point in the history
Fixes #2725 : move logging directory path config to CodeIgniter\Log\Handlers\FileHandler config
  • Loading branch information
lonnieezell authored Mar 22, 2020
2 parents 33b61a7 + f526510 commit dbc06d3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
17 changes: 8 additions & 9 deletions app/Config/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' => '',
],

/**
Expand Down
2 changes: 1 addition & 1 deletion system/Log/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '.');
Expand Down
15 changes: 5 additions & 10 deletions system/Test/Mock/MockLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ class MockLogger

public $threshold = 9;

/*
|--------------------------------------------------------------------------
| Error Logging Directory Path
|--------------------------------------------------------------------------
|
|
|
*/
public $path = '';

/*
|--------------------------------------------------------------------------
| Date Format for Logs
Expand Down Expand Up @@ -97,6 +87,11 @@ class MockLogger
'notice',
'warning',
],

/*
* Logging Directory Path
*/
'path' => '',
],
];

Expand Down
27 changes: 15 additions & 12 deletions tests/system/Log/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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);

Expand Down

0 comments on commit dbc06d3

Please sign in to comment.