mlog: use a waitgroup to flush the Logger on Stop
This commit is contained in:
parent
c66f13927a
commit
bc8f323514
14
mlog/mlog.go
14
mlog/mlog.go
@ -252,6 +252,7 @@ type Logger struct {
|
|||||||
msgBufPool *sync.Pool
|
msgBufPool *sync.Pool
|
||||||
msgCh chan msg
|
msgCh chan msg
|
||||||
testMsgWrittenCh chan struct{} // only initialized/used in tests
|
testMsgWrittenCh chan struct{} // only initialized/used in tests
|
||||||
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLogger initializes and returns a new instance of Logger which will write
|
// NewLogger initializes and returns a new instance of Logger which will write
|
||||||
@ -268,7 +269,11 @@ func NewLogger(wc io.WriteCloser) *Logger {
|
|||||||
msgCh: make(chan msg, 1024),
|
msgCh: make(chan msg, 1024),
|
||||||
maxLevel: InfoLevel.Uint(),
|
maxLevel: InfoLevel.Uint(),
|
||||||
}
|
}
|
||||||
go l.spin()
|
l.wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer l.wg.Done()
|
||||||
|
l.spin()
|
||||||
|
}()
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -328,10 +333,13 @@ func (l *Logger) WithKV(kvs ...KVer) *Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Stop stops and cleans up any running go-routines and resources held by the
|
// Stop stops and cleans up any running go-routines and resources held by the
|
||||||
// Logger, allowing it to be garbage-collected. The Logger should not be used
|
// Logger, allowing it to be garbage-collected. This will flush any remaining
|
||||||
// after Stop is called
|
// messages to the io.WriteCloser before returning.
|
||||||
|
//
|
||||||
|
// The Logger should not be used after Stop is called
|
||||||
func (l *Logger) Stop() {
|
func (l *Logger) Stop() {
|
||||||
close(l.msgCh)
|
close(l.msgCh)
|
||||||
|
l.wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log can be used to manually log a message of some custom defined Level. kvs
|
// Log can be used to manually log a message of some custom defined Level. kvs
|
||||||
|
Loading…
Reference in New Issue
Block a user