Skip to content

Commit

Permalink
log: fix daily log rotated (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier authored Mar 14, 2024
1 parent 8e68760 commit 0a40bf8
Showing 1 changed file with 10 additions and 6 deletions.
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

0 comments on commit 0a40bf8

Please sign in to comment.