Merge pull request #33305 from thaJeztah/suppress-logger-close-error

Don't log error if file is already closed
This commit is contained in:
Sebastiaan van Stijn 2017-05-31 17:09:09 +02:00 committed by GitHub
commit 316681cd2a
1 changed files with 18 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package loggerutils package loggerutils
import ( import (
"errors"
"os" "os"
"strconv" "strconv"
"sync" "sync"
@ -11,6 +12,7 @@ import (
// RotateFileWriter is Logger implementation for default Docker logging. // RotateFileWriter is Logger implementation for default Docker logging.
type RotateFileWriter struct { type RotateFileWriter struct {
f *os.File // store for closing f *os.File // store for closing
closed bool
mu sync.Mutex mu sync.Mutex
capacity int64 //maximum size of each file capacity int64 //maximum size of each file
currentSize int64 // current size of the latest file currentSize int64 // current size of the latest file
@ -42,6 +44,10 @@ func NewRotateFileWriter(logPath string, capacity int64, maxFiles int) (*RotateF
//WriteLog write log message to File //WriteLog write log message to File
func (w *RotateFileWriter) Write(message []byte) (int, error) { func (w *RotateFileWriter) Write(message []byte) (int, error) {
w.mu.Lock() w.mu.Lock()
if w.closed {
w.mu.Unlock()
return -1, errors.New("cannot write because the output file was closed")
}
if err := w.checkCapacityAndRotate(); err != nil { if err := w.checkCapacityAndRotate(); err != nil {
w.mu.Unlock() w.mu.Unlock()
return -1, err return -1, err
@ -100,6 +106,8 @@ func rotate(name string, maxFiles int) error {
// LogPath returns the location the given writer logs to. // LogPath returns the location the given writer logs to.
func (w *RotateFileWriter) LogPath() string { func (w *RotateFileWriter) LogPath() string {
w.mu.Lock()
defer w.mu.Unlock()
return w.f.Name() return w.f.Name()
} }
@ -120,5 +128,14 @@ func (w *RotateFileWriter) NotifyRotateEvict(sub chan interface{}) {
// Close closes underlying file and signals all readers to stop. // Close closes underlying file and signals all readers to stop.
func (w *RotateFileWriter) Close() error { func (w *RotateFileWriter) Close() error {
return w.f.Close() w.mu.Lock()
defer w.mu.Unlock()
if w.closed {
return nil
}
if err := w.f.Close(); err != nil {
return err
}
w.closed = true
return nil
} }