1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 06:01:29 +00:00

try to fix the little bug when calling Close or Flush in async mode

This commit is contained in:
youngsterxyf 2016-01-28 21:53:44 +08:00
parent 85c0fcd335
commit ecab397073

View File

@ -98,6 +98,8 @@ type BeeLogger struct {
loggerFuncCallDepth int loggerFuncCallDepth int
asynchronous bool asynchronous bool
msgChan chan *logMsg msgChan chan *logMsg
signalChan chan string
wg sync.WaitGroup
outputs []*nameLogger outputs []*nameLogger
} }
@ -122,6 +124,8 @@ func NewLogger(channelLen int64) *BeeLogger {
bl.level = LevelDebug bl.level = LevelDebug
bl.loggerFuncCallDepth = 2 bl.loggerFuncCallDepth = 2
bl.msgChan = make(chan *logMsg, channelLen) bl.msgChan = make(chan *logMsg, channelLen)
bl.signalChan = make(chan string, 1)
bl.wg.Add(1)
return bl return bl
} }
@ -237,6 +241,27 @@ func (bl *BeeLogger) startLogger() {
case bm := <-bl.msgChan: case bm := <-bl.msgChan:
bl.writeToLoggers(bm.when, bm.msg, bm.level) bl.writeToLoggers(bm.when, bm.msg, bm.level)
logMsgPool.Put(bm) logMsgPool.Put(bm)
case sg := <-bl.signalChan:
// Now should only send "flush" or "close" to bl.signalChan
for {
if len(bl.msgChan) > 0 {
bm := <-bl.msgChan
bl.writeToLoggers(bm.when, bm.msg, bm.level)
logMsgPool.Put(bm)
continue
}
break
}
for _, l := range bl.outputs {
l.Flush()
}
if sg == "close" {
for _, l := range bl.outputs {
l.Destroy()
}
bl.outputs = nil
}
bl.wg.Done()
} }
} }
} }
@ -345,27 +370,16 @@ func (bl *BeeLogger) Trace(format string, v ...interface{}) {
// Flush flush all chan data. // Flush flush all chan data.
func (bl *BeeLogger) Flush() { func (bl *BeeLogger) Flush() {
for _, l := range bl.outputs { bl.signalChan <- "flush"
l.Flush() bl.wg.Wait()
} bl.wg.Add(1)
} }
// Close close logger, flush all chan data and destroy all adapters in BeeLogger. // Close close logger, flush all chan data and destroy all adapters in BeeLogger.
func (bl *BeeLogger) Close() { func (bl *BeeLogger) Close() {
for { bl.signalChan <- "close"
if len(bl.msgChan) > 0 { bl.wg.Wait()
bm := <-bl.msgChan bl.wg.Add(1)
bl.writeToLoggers(bm.when, bm.msg, bm.level)
logMsgPool.Put(bm)
continue
}
break
}
for _, l := range bl.outputs {
l.Flush()
l.Destroy()
}
bl.outputs = nil
} }
func formatLogTime(when time.Time) string { func formatLogTime(when time.Time) string {