1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-08 06:06:40 +02:00

Merge branch '2.x' into main

This commit is contained in:
Jordi Boggiano
2022-06-09 11:04:38 +02:00
7 changed files with 172 additions and 14 deletions

View File

@@ -46,11 +46,38 @@ class RotatingFileHandlerTest extends TestCase
foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) {
unlink($file);
}
if ('testRotationWithFolderByDate' === $this->getName(false)) {
foreach (glob(__DIR__.'/Fixtures/[0-9]*') as $folder) {
$this->rrmdir($folder);
}
}
restore_error_handler();
unset($this->lastError);
}
private function rrmdir($directory) {
if (! is_dir($directory)) {
throw new InvalidArgumentException("$directory must be a directory");
}
if (substr($directory, strlen($directory) - 1, 1) !== '/') {
$directory .= '/';
}
foreach (glob($directory . '*', GLOB_MARK) as $path) {
if (is_dir($path)) {
$this->rrmdir($path);
} else {
unlink($path);
}
}
return rmdir($directory);
}
private function assertErrorWasTriggered($code, $message)
{
if (empty($this->lastError)) {
@@ -141,6 +168,76 @@ class RotatingFileHandlerTest extends TestCase
];
}
private function createDeep($file)
{
mkdir(dirname($file), 0777, true);
touch($file);
return $file;
}
/**
* @dataProvider rotationWithFolderByDateTests
*/
public function testRotationWithFolderByDate($createFile, $dateFormat, $timeCallback)
{
$old1 = $this->createDeep(__DIR__.'/Fixtures/'.date($dateFormat, $timeCallback(-1)).'/foo.rot');
$old2 = $this->createDeep(__DIR__.'/Fixtures/'.date($dateFormat, $timeCallback(-2)).'/foo.rot');
$old3 = $this->createDeep(__DIR__.'/Fixtures/'.date($dateFormat, $timeCallback(-3)).'/foo.rot');
$old4 = $this->createDeep(__DIR__.'/Fixtures/'.date($dateFormat, $timeCallback(-4)).'/foo.rot');
$log = __DIR__.'/Fixtures/'.date($dateFormat).'/foo.rot';
if ($createFile) {
$this->createDeep($log);
}
$handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
$handler->setFormatter($this->getIdentityFormatter());
$handler->setFilenameFormat('{date}/{filename}', $dateFormat);
$handler->handle($this->getRecord());
$handler->close();
$this->assertTrue(file_exists($log));
$this->assertTrue(file_exists($old1));
$this->assertEquals($createFile, file_exists($old2));
$this->assertEquals($createFile, file_exists($old3));
$this->assertEquals($createFile, file_exists($old4));
$this->assertEquals('test', file_get_contents($log));
}
public function rotationWithFolderByDateTests()
{
$now = time();
$dayCallback = function ($ago) use ($now) {
return $now + 86400 * $ago;
};
$monthCallback = function ($ago) {
return gmmktime(0, 0, 0, (int) (date('n') + $ago), 1, (int) date('Y'));
};
$yearCallback = function ($ago) {
return gmmktime(0, 0, 0, 1, 1, (int) (date('Y') + $ago));
};
return [
'Rotation is triggered when the file of the current day is not present'
=> [true, 'Y/m/d', $dayCallback],
'Rotation is not triggered when the file of the current day is already present'
=> [false, 'Y/m/d', $dayCallback],
'Rotation is triggered when the file of the current month is not present'
=> [true, 'Y/m', $monthCallback],
'Rotation is not triggered when the file of the current month is already present'
=> [false, 'Y/m', $monthCallback],
'Rotation is triggered when the file of the current year is not present'
=> [true, 'Y', $yearCallback],
'Rotation is not triggered when the file of the current year is already present'
=> [false, 'Y', $yearCallback],
];
}
/**
* @dataProvider dateFormatProvider
*/
@@ -205,6 +302,7 @@ class RotatingFileHandlerTest extends TestCase
['foobar-{date}', true],
['foo-{date}-bar', true],
['{date}-foobar', true],
['{date}/{filename}', true],
['foobar', false],
];
}

View File

@@ -740,6 +740,28 @@ class LoggerTest extends TestCase
$this->assertNotSame($uid1, $processorUid1->getUid());
$this->assertNotSame($uid2, $processorUid2->getUid());
}
/**
* @covers Logger::addRecord
*/
public function testLogWithDateTime()
{
foreach ([true, false] as $microseconds) {
$logger = new Logger(__METHOD__);
$loggingHandler = new LoggingHandler($logger);
$testHandler = new TestHandler();
$logger->pushHandler($loggingHandler);
$logger->pushHandler($testHandler);
$datetime = (new DateTimeImmutable($microseconds))->modify('2022-03-04 05:06:07');
$logger->addRecord(Level::Debug, 'test', [], $datetime);
list($record) = $testHandler->getRecords();
$this->assertEquals($datetime->format('Y-m-d H:i:s'), $record->datetime->format('Y-m-d H:i:s'));
}
}
}
class LoggingHandler implements HandlerInterface