mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-08 06:06:40 +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:
@@ -69,11 +69,12 @@ class RotatingFileHandler extends StreamHandler
|
|||||||
|
|
||||||
public function setFilenameFormat($filenameFormat, $dateFormat)
|
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(
|
throw new InvalidArgumentException(
|
||||||
'Invalid date format - format must be one of '.
|
'Invalid date format - format must be one of '.
|
||||||
'RotatingFileHandler::FILE_PER_DAY, RotatingFileHandler::FILE_PER_MONTH '.
|
'RotatingFileHandler::FILE_PER_DAY ("Y-m-d"), RotatingFileHandler::FILE_PER_MONTH ("Y-m")'.
|
||||||
'or RotatingFileHandler::FILE_PER_YEAR.'
|
'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) {
|
if (substr_count($filenameFormat, '{date}') === 0) {
|
||||||
|
@@ -153,8 +153,20 @@ class RotatingFileHandlerTest extends TestCase
|
|||||||
[RotatingFileHandler::FILE_PER_DAY, true],
|
[RotatingFileHandler::FILE_PER_DAY, true],
|
||||||
[RotatingFileHandler::FILE_PER_MONTH, true],
|
[RotatingFileHandler::FILE_PER_MONTH, true],
|
||||||
[RotatingFileHandler::FILE_PER_YEAR, 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],
|
['m-d-Y', false],
|
||||||
['Y-m-d-h-i', false],
|
['Y-m-d-h-i', false],
|
||||||
|
['Y-', false],
|
||||||
|
['Y-m-', false],
|
||||||
|
['Y--', false],
|
||||||
|
['m-d', false],
|
||||||
|
['Y-d', false]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user