From 2b1316c73826c8bfdbe34fa194f18770930599af Mon Sep 17 00:00:00 2001 From: JessonChan Date: Wed, 16 Mar 2016 18:04:07 +0800 Subject: [PATCH] data race bug fixed --- logs/file.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/logs/file.go b/logs/file.go index 9d3f78a0..672823fa 100644 --- a/logs/file.go +++ b/logs/file.go @@ -30,7 +30,7 @@ import ( // fileLogWriter implements LoggerInterface. // It writes messages by lines limit, file size limit, or time frequency. type fileLogWriter struct { - sync.Mutex // write log order by order and atomic incr maxLinesCurLines and maxSizeCurSize + sync.RWMutex // write log order by order and atomic incr maxLinesCurLines and maxSizeCurSize // The opened file Filename string `json:"filename"` fileWriter *os.File @@ -77,7 +77,7 @@ func newFileWriter() Logger { // { // "filename":"logs/beego.log", // "maxLines":10000, -// "maxsize":1<<30, +// "maxsize":1024, // "daily":true, // "maxDays":15, // "rotate":true, @@ -128,7 +128,9 @@ func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error { h, d := formatTimeHeader(when) msg = string(h) + msg + "\n" if w.Rotate { + w.RLock() if w.needRotate(len(msg), d) { + w.RUnlock() w.Lock() if w.needRotate(len(msg), d) { if err := w.doRotate(when); err != nil { @@ -136,6 +138,8 @@ func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error { } } w.Unlock() + } else { + w.RUnlock() } }