1
0
mirror of https://github.com/astaxie/beego.git synced 2025-07-06 05:50:19 +00:00

file rotate by day test

This commit is contained in:
JessonChan
2016-05-06 12:09:00 +08:00
parent 4d8e1f93ff
commit fa8d94fa69
2 changed files with 67 additions and 0 deletions

View File

@ -174,9 +174,28 @@ func (w *fileLogWriter) initFd() error {
}
w.maxLinesCurLines = count
}
if w.Daily {
go w.dailyRotate(w.dailyOpenTime)
}
return nil
}
func (w *fileLogWriter) dailyRotate(openTime time.Time) {
y, m, d := openTime.Add(24 * time.Hour).Date()
nextDay := time.Date(y, m, d, 0, 0, 0, 0, openTime.Location())
tm := time.NewTimer(time.Duration(nextDay.UnixNano() - openTime.UnixNano() + 100))
select {
case <-tm.C:
w.Lock()
if w.needRotate(0, time.Now().Day()) {
if err := w.doRotate(time.Now()); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
}
}
w.Unlock()
}
}
func (w *fileLogWriter) lines() (int, error) {
fd, err := os.Open(w.Filename)
if err != nil {