Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support yearly or monthly rotation log file #1805

Merged
merged 4 commits into from
Jun 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions src/Monolog/Handler/RotatingFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ class RotatingFileHandler extends StreamHandler
* @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
* @param bool $useLocking Try to lock log file before doing any writes
*/
public function __construct(string $filename, int $maxFiles = 0, int|string|Level $level = Level::Debug, bool $bubble = true, ?int $filePermission = null, bool $useLocking = false)
public function __construct(string $filename, int $maxFiles = 0, int|string|Level $level = Level::Debug, bool $bubble = true, ?int $filePermission = null, bool $useLocking = false, string $dateFormat = self::FILE_PER_DAY, string $filenameFormat = '{filename}-{date}')
{
$this->filename = Utils::canonicalizePath($filename);
$this->maxFiles = $maxFiles;
$this->nextRotation = new \DateTimeImmutable('tomorrow');
$this->filenameFormat = '{filename}-{date}';
$this->dateFormat = static::FILE_PER_DAY;
$this->setFilenameFormat($filenameFormat, $dateFormat);
$this->nextRotation = $this->getNextRotation();

parent::__construct($this->getTimedFilename(), $level, $bubble, $filePermission, $useLocking);
}
Expand Down Expand Up @@ -80,21 +79,13 @@ public function reset(): void

public function setFilenameFormat(string $filenameFormat, string $dateFormat): self
{
if (0 === preg_match('{^[Yy](([/_.-]?m)([/_.-]?d)?)?$}', $dateFormat)) {
throw new InvalidArgumentException(
'Invalid date format - format must be one of '.
'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 using slashes, underscores and/or dots instead of dashes.'
);
}
$this->setDateFormat($dateFormat);
if (substr_count($filenameFormat, '{date}') === 0) {
throw new InvalidArgumentException(
'Invalid filename format - format must contain at least `{date}`, because otherwise rotating is impossible.'
);
}
$this->filenameFormat = $filenameFormat;
$this->dateFormat = $dateFormat;
$this->url = $this->getTimedFilename();
$this->close();

Expand Down Expand Up @@ -126,7 +117,7 @@ protected function rotate(): void
{
// update filename
$this->url = $this->getTimedFilename();
$this->nextRotation = new \DateTimeImmutable('tomorrow');
$this->nextRotation = $this->getNextRotation();

// skip GC of old logs if files are unlimited
if (0 === $this->maxFiles) {
Expand Down Expand Up @@ -198,4 +189,26 @@ protected function getGlobPattern(): string

return $glob;
}

protected function setDateFormat(string $dateFormat): void
{
if (0 === preg_match('{^[Yy](([/_.-]?m)([/_.-]?d)?)?$}', $dateFormat)) {
throw new InvalidArgumentException(
'Invalid date format - format must be one of '.
'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 using slashes, underscores and/or dots instead of dashes.'
);
}
$this->dateFormat = $dateFormat;
}

protected function getNextRotation(): \DateTimeImmutable
{
return match (str_replace(['/','_','.'], '-', $this->dateFormat)) {
self::FILE_PER_MONTH => (new \DateTimeImmutable('first day of next month'))->setTime(0, 0, 0),
self::FILE_PER_YEAR => (new \DateTimeImmutable('first day of January next year'))->setTime(0, 0, 0),
default => (new \DateTimeImmutable('tomorrow'))->setTime(0, 0, 0),
};
}
}