make the json log writer much faster

Signed-off-by: Shijiang Wei <mountkin@gmail.com>
This commit is contained in:
Shijiang Wei 2016-02-23 19:28:04 +08:00
Родитель 6668326aa8
Коммит d7af031114
2 изменённых файлов: 19 добавлений и 10 удалений

Просмотреть файл

@ -94,7 +94,6 @@ func (l *JSONFileLogger) Log(msg *logger.Message) error {
return err return err
} }
l.mu.Lock() l.mu.Lock()
defer l.mu.Unlock()
err = (&jsonlog.JSONLogs{ err = (&jsonlog.JSONLogs{
Log: append(msg.Line, '\n'), Log: append(msg.Line, '\n'),
Stream: msg.Source, Stream: msg.Source,
@ -102,6 +101,7 @@ func (l *JSONFileLogger) Log(msg *logger.Message) error {
RawAttrs: l.extra, RawAttrs: l.extra,
}).MarshalJSONBuf(l.buf) }).MarshalJSONBuf(l.buf)
if err != nil { if err != nil {
l.mu.Unlock()
return err return err
} }
@ -109,6 +109,7 @@ func (l *JSONFileLogger) Log(msg *logger.Message) error {
_, err = l.writer.Write(l.buf.Bytes()) _, err = l.writer.Write(l.buf.Bytes())
l.writeNotifier.Publish(struct{}{}) l.writeNotifier.Publish(struct{}{})
l.buf.Reset() l.buf.Reset()
l.mu.Unlock()
return err return err
} }

Просмотреть файл

@ -13,6 +13,7 @@ type RotateFileWriter struct {
f *os.File // store for closing f *os.File // store for closing
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
maxFiles int //maximum number of files maxFiles int //maximum number of files
notifyRotate *pubsub.Publisher notifyRotate *pubsub.Publisher
} }
@ -21,12 +22,18 @@ type RotateFileWriter struct {
func NewRotateFileWriter(logPath string, capacity int64, maxFiles int) (*RotateFileWriter, error) { func NewRotateFileWriter(logPath string, capacity int64, maxFiles int) (*RotateFileWriter, error) {
log, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640) log, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640)
if err != nil { if err != nil {
return &RotateFileWriter{}, err return nil, err
}
size, err := log.Seek(0, os.SEEK_END)
if err != nil {
return nil, err
} }
return &RotateFileWriter{ return &RotateFileWriter{
f: log, f: log,
capacity: capacity, capacity: capacity,
currentSize: size,
maxFiles: maxFiles, maxFiles: maxFiles,
notifyRotate: pubsub.NewPublisher(0, 1), notifyRotate: pubsub.NewPublisher(0, 1),
}, nil }, nil
@ -35,12 +42,17 @@ 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()
defer w.mu.Unlock()
if err := w.checkCapacityAndRotate(); err != nil { if err := w.checkCapacityAndRotate(); err != nil {
w.mu.Unlock()
return -1, err return -1, err
} }
return w.f.Write(message) n, err := w.f.Write(message)
if err == nil {
w.currentSize += int64(n)
}
w.mu.Unlock()
return n, err
} }
func (w *RotateFileWriter) checkCapacityAndRotate() error { func (w *RotateFileWriter) checkCapacityAndRotate() error {
@ -48,12 +60,7 @@ func (w *RotateFileWriter) checkCapacityAndRotate() error {
return nil return nil
} }
meta, err := w.f.Stat() if w.currentSize >= w.capacity {
if err != nil {
return err
}
if meta.Size() >= w.capacity {
name := w.f.Name() name := w.f.Name()
if err := w.f.Close(); err != nil { if err := w.f.Close(); err != nil {
return err return err
@ -66,6 +73,7 @@ func (w *RotateFileWriter) checkCapacityAndRotate() error {
return err return err
} }
w.f = file w.f = file
w.currentSize = 0
w.notifyRotate.Publish(struct{}{}) w.notifyRotate.Publish(struct{}{})
} }