Skip to content

Fix duplicate rotation triggered by Write() after manual Rotate()/RotateWithReason()#84

Merged
DeRuina merged 3 commits intomainfrom
copilot/fix-rotatewithreason-bug
Mar 11, 2026
Merged

Fix duplicate rotation triggered by Write() after manual Rotate()/RotateWithReason()#84
DeRuina merged 3 commits intomainfrom
copilot/fix-rotatewithreason-bug

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 11, 2026

Rotate() and RotateWithReason() never updated lastRotationTime, so the very next Write() would re-evaluate the interval/scheduled guards, find them still expired, and fire a second unwanted rotation.

Changes

  • timberjack.go — after a successful l.rotate(r), set l.lastRotationTime = l.resolvedTimeNow() so Write()'s interval and scheduled-time guards see the rotation as already completed. Updated doc comment to reflect the new behaviour.
  • timberjack_test.go — added two regression tests:
    • TestRotate_NoDuplicateRotationOnNextWrite
    • TestRotateWithReason_NoDuplicateRotationOnNextWrite

Example

l := &Logger{Filename: "app.log", RotationInterval: time.Hour}
l.Write([]byte("boot"))         // initialises lastRotationTime

// two hours later — interval is due
l.Rotate()                       // rotates → before fix, lastRotationTime NOT updated
l.Write([]byte("next entry"))   // before fix: triggers a SECOND rotation immediately
                                 // after fix:  writes normally, no extra rotation

Copilot AI and others added 2 commits March 11, 2026 07:11
…r rotation

Co-authored-by: DeRuina <81315494+DeRuina@users.noreply.github.com>
Co-authored-by: DeRuina <81315494+DeRuina@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix potential bug regarding RotateWithReason and Rotate Fix duplicate rotation triggered by Write() after manual Rotate()/RotateWithReason() Mar 11, 2026
@DeRuina DeRuina marked this pull request as ready for review March 11, 2026 07:24
Copy link
Copy Markdown
Owner

@DeRuina DeRuina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Owner

@DeRuina DeRuina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@DeRuina DeRuina merged commit 5aee542 into main Mar 11, 2026
4 checks passed
@DeRuina DeRuina deleted the copilot/fix-rotatewithreason-bug branch March 11, 2026 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants