Skip to content

Commit 0d529a7

Browse files
authored
Rotate files based on timezone (#1982)
1 parent 78e6df0 commit 0d529a7

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/Monolog/Handler/RotatingFileHandler.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Monolog\Handler;
1313

14+
use DateTimeZone;
1415
use InvalidArgumentException;
1516
use Monolog\Level;
1617
use Monolog\Utils;
@@ -37,18 +38,20 @@ class RotatingFileHandler extends StreamHandler
3738
protected \DateTimeImmutable $nextRotation;
3839
protected string $filenameFormat;
3940
protected string $dateFormat;
41+
protected DateTimeZone|null $timezone = null;
4042

4143
/**
4244
* @param int $maxFiles The maximal amount of files to keep (0 means unlimited)
4345
* @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
4446
* @param bool $useLocking Try to lock log file before doing any writes
4547
*/
46-
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}')
48+
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}', DateTimeZone|null $timezone = null)
4749
{
4850
$this->filename = Utils::canonicalizePath($filename);
4951
$this->maxFiles = $maxFiles;
5052
$this->setFilenameFormat($filenameFormat, $dateFormat);
5153
$this->nextRotation = $this->getNextRotation();
54+
$this->timezone = $timezone;
5255

5356
parent::__construct($this->getTimedFilename(), $level, $bubble, $filePermission, $useLocking);
5457
}
@@ -177,7 +180,7 @@ protected function getTimedFilename(): string
177180
$fileInfo = pathinfo($this->filename);
178181
$timedFilename = str_replace(
179182
['{filename}', '{date}'],
180-
[$fileInfo['filename'], date($this->dateFormat)],
183+
[$fileInfo['filename'], (new \DateTimeImmutable(timezone: $this->timezone))->format($this->dateFormat)],
181184
($fileInfo['dirname'] ?? '') . '/' . $this->filenameFormat
182185
);
183186

0 commit comments

Comments
 (0)