diff --git a/lumberjack.go b/lumberjack.go index a47b7f0..edc8162 100644 --- a/lumberjack.go +++ b/lumberjack.go @@ -111,8 +111,7 @@ type Logger struct { file *os.File mu sync.Mutex - millCh chan bool - startMill sync.Once + millCh chan struct{} } var ( @@ -175,6 +174,10 @@ func (l *Logger) close() error { } err := l.file.Close() l.file = nil + if l.millCh != nil { + close(l.millCh) + l.millCh = nil + } return err } @@ -375,8 +378,8 @@ func (l *Logger) millRunOnce() error { // millRun runs in a goroutine to manage post-rotation compression and removal // of old log files. -func (l *Logger) millRun() { - for _ = range l.millCh { +func (l *Logger) millRun(ch <-chan struct{}) { + for range ch { // what am I going to do, log this? _ = l.millRunOnce() } @@ -385,12 +388,13 @@ func (l *Logger) millRun() { // mill performs post-rotation compression and removal of stale log files, // starting the mill goroutine if necessary. func (l *Logger) mill() { - l.startMill.Do(func() { - l.millCh = make(chan bool, 1) - go l.millRun() - }) + // It is safe to check the millCh here as we are inside the mutex lock. + if l.millCh == nil { + l.millCh = make(chan struct{}, 1) + go l.millRun(l.millCh) + } select { - case l.millCh <- true: + case l.millCh <- struct{}{}: default: } }