rft: motify BeeLogger signalChan (#5139)

This commit is contained in:
Stone-afk 2023-01-20 13:49:03 +08:00 committed by Ming Deng
parent e455869ef6
commit fb76377a3e
3 changed files with 19 additions and 13 deletions

View File

@ -6,6 +6,7 @@
- [Fix 5129: must set formatter after init the logger](https://github.com/beego/beego/pull/5130) - [Fix 5129: must set formatter after init the logger](https://github.com/beego/beego/pull/5130)
- [Fix 5079: only log msg when the channel is not closed](https://github.com/beego/beego/pull/5132) - [Fix 5079: only log msg when the channel is not closed](https://github.com/beego/beego/pull/5132)
- [Fix 4435: Controller SaveToFile remove all temp file](https://github.com/beego/beego/pull/5138) - [Fix 4435: Controller SaveToFile remove all temp file](https://github.com/beego/beego/pull/5138)
- [Fix 5079: Split signalChan into flushChan and closeChan](https://github.com/beego/beego/pull/5139)
# v2.0.7 # v2.0.7
- [Upgrade github.com/go-kit/kit, CVE-2022-24450](https://github.com/beego/beego/pull/5121) - [Upgrade github.com/go-kit/kit, CVE-2022-24450](https://github.com/beego/beego/pull/5121)

View File

@ -63,6 +63,8 @@ func TestConsoleAsync(t *testing.T) {
for len(log.msgChan) != 0 { for len(log.msgChan) != 0 {
time.Sleep(1 * time.Millisecond) time.Sleep(1 * time.Millisecond)
} }
log.Flush()
log.Close()
} }
func TestFormat(t *testing.T) { func TestFormat(t *testing.T) {

View File

@ -121,7 +121,8 @@ type BeeLogger struct {
prefix string prefix string
msgChanLen int64 msgChanLen int64
msgChan chan *LogMsg msgChan chan *LogMsg
signalChan chan string closeChan chan struct{}
flushChan chan struct{}
outputs []*nameLogger outputs []*nameLogger
globalFormatter string globalFormatter string
} }
@ -146,7 +147,8 @@ func NewLogger(channelLens ...int64) *BeeLogger {
if bl.msgChanLen <= 0 { if bl.msgChanLen <= 0 {
bl.msgChanLen = defaultAsyncMsgLen bl.msgChanLen = defaultAsyncMsgLen
} }
bl.signalChan = make(chan string, 1) bl.flushChan = make(chan struct{}, 1)
bl.closeChan = make(chan struct{}, 1)
bl.setLogger(AdapterConsole) bl.setLogger(AdapterConsole)
return bl return bl
} }
@ -366,16 +368,16 @@ func (bl *BeeLogger) startLogger() {
bl.writeToLoggers(bm) bl.writeToLoggers(bm)
logMsgPool.Put(bm) logMsgPool.Put(bm)
} }
case sg := <-bl.signalChan: case <-bl.closeChan:
// Now should only send "flush" or "close" to bl.signalChan
bl.flush() bl.flush()
if sg == "close" { for _, l := range bl.outputs {
for _, l := range bl.outputs { l.Destroy()
l.Destroy()
}
bl.outputs = nil
gameOver = true
} }
bl.outputs = nil
gameOver = true
bl.wg.Done()
case <-bl.flushChan:
bl.flush()
bl.wg.Done() bl.wg.Done()
} }
if gameOver { if gameOver {
@ -569,7 +571,7 @@ 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() {
if bl.asynchronous { if bl.asynchronous {
bl.signalChan <- "flush" bl.flushChan <- struct{}{}
bl.wg.Wait() bl.wg.Wait()
bl.wg.Add(1) bl.wg.Add(1)
return return
@ -580,7 +582,7 @@ 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() {
if bl.asynchronous { if bl.asynchronous {
bl.signalChan <- "close" bl.closeChan <- struct{}{}
bl.wg.Wait() bl.wg.Wait()
close(bl.msgChan) close(bl.msgChan)
} else { } else {
@ -590,7 +592,8 @@ func (bl *BeeLogger) Close() {
} }
bl.outputs = nil bl.outputs = nil
} }
close(bl.signalChan) close(bl.flushChan)
close(bl.closeChan)
} }
// Reset close all outputs, and set bl.outputs to nil // Reset close all outputs, and set bl.outputs to nil