mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 20:20:56 +00:00
use pool to logMsg
This commit is contained in:
parent
164366ae0d
commit
69804afc1b
23
logs/log.go
23
logs/log.go
@ -95,7 +95,7 @@ type BeeLogger struct {
|
|||||||
enableFuncCallDepth bool
|
enableFuncCallDepth bool
|
||||||
loggerFuncCallDepth int
|
loggerFuncCallDepth int
|
||||||
asynchronous bool
|
asynchronous bool
|
||||||
msg chan *logMsg
|
msgChan chan *logMsg
|
||||||
outputs []*nameLogger
|
outputs []*nameLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,6 +109,8 @@ type logMsg struct {
|
|||||||
msg string
|
msg string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var logMsgPool *sync.Pool
|
||||||
|
|
||||||
// NewLogger returns a new BeeLogger.
|
// NewLogger returns a new BeeLogger.
|
||||||
// channelLen means the number of messages in chan(used where asynchronous is true).
|
// channelLen means the number of messages in chan(used where asynchronous is true).
|
||||||
// if the buffering chan is full, logger adapters write to file or other way.
|
// if the buffering chan is full, logger adapters write to file or other way.
|
||||||
@ -116,13 +118,18 @@ func NewLogger(channelLen int64) *BeeLogger {
|
|||||||
bl := new(BeeLogger)
|
bl := new(BeeLogger)
|
||||||
bl.level = LevelDebug
|
bl.level = LevelDebug
|
||||||
bl.loggerFuncCallDepth = 2
|
bl.loggerFuncCallDepth = 2
|
||||||
bl.msg = make(chan *logMsg, channelLen)
|
bl.msgChan = make(chan *logMsg, channelLen)
|
||||||
return bl
|
return bl
|
||||||
}
|
}
|
||||||
|
|
||||||
// Async set the log to asynchronous and start the goroutine
|
// Async set the log to asynchronous and start the goroutine
|
||||||
func (bl *BeeLogger) Async() *BeeLogger {
|
func (bl *BeeLogger) Async() *BeeLogger {
|
||||||
bl.asynchronous = true
|
bl.asynchronous = true
|
||||||
|
logMsgPool = &sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return &logMsg{}
|
||||||
|
},
|
||||||
|
}
|
||||||
go bl.startLogger()
|
go bl.startLogger()
|
||||||
return bl
|
return bl
|
||||||
}
|
}
|
||||||
@ -187,10 +194,10 @@ func (bl *BeeLogger) writeMsg(logLevel int, msg string) error {
|
|||||||
msg = msg
|
msg = msg
|
||||||
}
|
}
|
||||||
if bl.asynchronous {
|
if bl.asynchronous {
|
||||||
lm := new(logMsg)
|
lm := logMsgPool.Get().(*logMsg)
|
||||||
lm.level = logLevel
|
lm.level = logLevel
|
||||||
lm.msg = msg
|
lm.msg = msg
|
||||||
bl.msg <- lm
|
bl.msgChan <- lm
|
||||||
} else {
|
} else {
|
||||||
bl.writeToLoggers(msg, logLevel)
|
bl.writeToLoggers(msg, logLevel)
|
||||||
}
|
}
|
||||||
@ -224,8 +231,9 @@ func (bl *BeeLogger) EnableFuncCallDepth(b bool) {
|
|||||||
func (bl *BeeLogger) startLogger() {
|
func (bl *BeeLogger) startLogger() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case bm := <-bl.msg:
|
case bm := <-bl.msgChan:
|
||||||
bl.writeToLoggers(bm.msg, bm.level)
|
bl.writeToLoggers(bm.msg, bm.level)
|
||||||
|
logMsgPool.Put(bm)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -342,9 +350,10 @@ func (bl *BeeLogger) Flush() {
|
|||||||
// 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 {
|
for {
|
||||||
if len(bl.msg) > 0 {
|
if len(bl.msgChan) > 0 {
|
||||||
bm := <-bl.msg
|
bm := <-bl.msgChan
|
||||||
bl.writeToLoggers(bm.msg, bm.level)
|
bl.writeToLoggers(bm.msg, bm.level)
|
||||||
|
logMsgPool.Put(bm)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
Loading…
Reference in New Issue
Block a user