mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Don't log error if file is already closed
When closing the log-file, and the file is already closed, there's no need to log an error. This patch adds a `closed` boolean to check if the file was closed, and if so, skip closing the file. This prevents errors like this being logged: level=error msg="Error closing logger: invalid argument" Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
d192db0d93
commit
07b51ed300
1 changed files with 18 additions and 1 deletions
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue