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

log: fix daily log rotated #28

Merged
merged 1 commit into from
Mar 14, 2024
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
16 changes: 10 additions & 6 deletions log/output_rotatefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,20 @@ func (fw *RotateFileWriter) dailyRotate() {
fw.mu.Unlock()
for {
now := fw.clock.Now()
y, m, d := now.Add(24 * time.Hour).Date()
nextDay := time.Date(y, m, d, 0, 0, 0, 0, now.Location())
tm := time.NewTimer(time.Duration(nextDay.UnixNano()-now.UnixNano()) + time.Millisecond)
// Calculate the time difference until the next hour.
nextHour := now.Truncate(time.Hour).Add(time.Hour)
select {
case <-tm.C:
fw.Rotate()
case <-time.After(nextHour.Sub(now)):
case <-doneCh:
tm.Stop()
return
}

// Rotate the log file at 0 hour of the day.
if fw.clock.Now().Hour() == 0 {
fw.Rotate()
// Ensure it's executed only once, even if the waiting period crosses midnight.
time.Sleep(time.Minute)
}
}
}

Expand Down