Skip to content

Commit

Permalink
Fix RotatingFileHandler bug where rotation could sometimes not happen…
Browse files Browse the repository at this point in the history
… correctly, fixes #1905
  • Loading branch information
Seldaek committed Nov 11, 2024
1 parent 0779fb9 commit d57089b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Monolog/Handler/RotatingFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ protected function write(array $record): void
// on the first record written, if the log is new, we should rotate (once per day)
if (null === $this->mustRotate) {
$this->mustRotate = null === $this->url || !file_exists($this->url);
if ($this->mustRotate) {
$this->close(); // triggers rotation
}
}

if ($this->nextRotation <= $record['datetime']) {
$this->mustRotate = true;
$this->close();
$this->close(); // triggers rotation
}

parent::write($record);
Expand All @@ -134,6 +137,8 @@ protected function rotate(): void
$this->url = $this->getTimedFilename();
$this->nextRotation = new \DateTimeImmutable('tomorrow');

$this->mustRotate = false;

// skip GC of old logs if files are unlimited
if (0 === $this->maxFiles) {
return;
Expand Down Expand Up @@ -166,8 +171,6 @@ protected function rotate(): void
restore_error_handler();
}
}

$this->mustRotate = false;
}

protected function getTimedFilename(): string
Expand Down

0 comments on commit d57089b

Please sign in to comment.