1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 07:51:30 +00:00

data race bug fixed

This commit is contained in:
JessonChan 2016-03-16 18:04:07 +08:00
parent c8bbfb75f0
commit 2b1316c738

View File

@ -30,7 +30,7 @@ import (
// fileLogWriter implements LoggerInterface. // fileLogWriter implements LoggerInterface.
// It writes messages by lines limit, file size limit, or time frequency. // It writes messages by lines limit, file size limit, or time frequency.
type fileLogWriter struct { 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 // The opened file
Filename string `json:"filename"` Filename string `json:"filename"`
fileWriter *os.File fileWriter *os.File
@ -77,7 +77,7 @@ func newFileWriter() Logger {
// { // {
// "filename":"logs/beego.log", // "filename":"logs/beego.log",
// "maxLines":10000, // "maxLines":10000,
// "maxsize":1<<30, // "maxsize":1024,
// "daily":true, // "daily":true,
// "maxDays":15, // "maxDays":15,
// "rotate":true, // "rotate":true,
@ -128,7 +128,9 @@ func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error {
h, d := formatTimeHeader(when) h, d := formatTimeHeader(when)
msg = string(h) + msg + "\n" msg = string(h) + msg + "\n"
if w.Rotate { if w.Rotate {
w.RLock()
if w.needRotate(len(msg), d) { if w.needRotate(len(msg), d) {
w.RUnlock()
w.Lock() w.Lock()
if w.needRotate(len(msg), d) { if w.needRotate(len(msg), d) {
if err := w.doRotate(when); err != nil { 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() w.Unlock()
} else {
w.RUnlock()
} }
} }