mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #11472 from LK4D4/logger_mutex
Protect jsonfilelog writes with mutex
This commit is contained in:
commit
54232084c5
1 changed files with 11 additions and 2 deletions
|
@ -3,6 +3,7 @@ package jsonfilelog
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/docker/docker/daemon/logger"
|
"github.com/docker/docker/daemon/logger"
|
||||||
"github.com/docker/docker/pkg/jsonlog"
|
"github.com/docker/docker/pkg/jsonlog"
|
||||||
|
@ -12,7 +13,8 @@ import (
|
||||||
// JSON objects to file
|
// JSON objects to file
|
||||||
type JSONFileLogger struct {
|
type JSONFileLogger struct {
|
||||||
buf *bytes.Buffer
|
buf *bytes.Buffer
|
||||||
f *os.File // store for closing
|
f *os.File // store for closing
|
||||||
|
mu sync.Mutex // protects buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates new JSONFileLogger which writes to filename
|
// New creates new JSONFileLogger which writes to filename
|
||||||
|
@ -29,13 +31,20 @@ func New(filename string) (logger.Logger, error) {
|
||||||
|
|
||||||
// Log converts logger.Message to jsonlog.JSONLog and serializes it to file
|
// Log converts logger.Message to jsonlog.JSONLog and serializes it to file
|
||||||
func (l *JSONFileLogger) Log(msg *logger.Message) error {
|
func (l *JSONFileLogger) Log(msg *logger.Message) error {
|
||||||
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
err := (&jsonlog.JSONLog{Log: string(msg.Line) + "\n", Stream: msg.Source, Created: msg.Timestamp}).MarshalJSONBuf(l.buf)
|
err := (&jsonlog.JSONLog{Log: string(msg.Line) + "\n", Stream: msg.Source, Created: msg.Timestamp}).MarshalJSONBuf(l.buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
l.buf.WriteByte('\n')
|
l.buf.WriteByte('\n')
|
||||||
_, err = l.buf.WriteTo(l.f)
|
_, err = l.buf.WriteTo(l.f)
|
||||||
return err
|
if err != nil {
|
||||||
|
// this buffer is screwed, replace it with another to avoid races
|
||||||
|
l.buf = bytes.NewBuffer(nil)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes underlying file
|
// Close closes underlying file
|
||||||
|
|
Loading…
Add table
Reference in a new issue