1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-07 13:46:38 +02:00

Allow alternative date separators for RotatingFileHandler

Currently only dashes are allowed in date formats (i.e., "Y-m-d").
This commit also allows slashes, underscores and dots to be used instead
of only dashes for more flexibility.
This commit is contained in:
Remon van de Kamp
2016-07-14 21:01:35 +02:00
parent f16e67d523
commit 49d64e1dd5
2 changed files with 16 additions and 3 deletions

View File

@@ -69,11 +69,12 @@ class RotatingFileHandler extends StreamHandler
public function setFilenameFormat($filenameFormat, $dateFormat)
{
if (!in_array($dateFormat, [self::FILE_PER_DAY, self::FILE_PER_MONTH, self::FILE_PER_YEAR])) {
if (!preg_match('~^Y(([/_\.-]m)([/_\.-]d)?)?$~', $dateFormat)) {
throw new InvalidArgumentException(
'Invalid date format - format must be one of '.
'RotatingFileHandler::FILE_PER_DAY, RotatingFileHandler::FILE_PER_MONTH '.
'or RotatingFileHandler::FILE_PER_YEAR.'
'RotatingFileHandler::FILE_PER_DAY ("Y-m-d"), RotatingFileHandler::FILE_PER_MONTH ("Y-m")'.
'or RotatingFileHandler::FILE_PER_YEAR ("Y"), or you can set one of the '.
'date formats where dashes are replaces with slashes, underscores and/or dots.'
);
}
if (substr_count($filenameFormat, '{date}') === 0) {

View File

@@ -153,8 +153,20 @@ class RotatingFileHandlerTest extends TestCase
[RotatingFileHandler::FILE_PER_DAY, true],
[RotatingFileHandler::FILE_PER_MONTH, true],
[RotatingFileHandler::FILE_PER_YEAR, true],
['Y/m/d', true],
['Y.m.d', true],
['Y_m_d', true],
['Y/m', true],
['Y.m', true],
['Y_m', true],
['', false],
['m-d-Y', false],
['Y-m-d-h-i', false],
['Y-', false],
['Y-m-', false],
['Y--', false],
['m-d', false],
['Y-d', false]
];
}