From 4e445d6351cb7f9910630aa55325d3b1ecc3931a Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 17 Jun 2022 05:15:24 +0100 Subject: [PATCH 1/2] Empty log queue on flush and close It is possible for log events to remain in the buffer off the multichannelledlog and thus not be logged despite close or flush. This PR simply adds a function to empty the queue before closing or flushing. (Except when the logger is paused.) Reference #19982 Signed-off-by: Andrew Thornton --- modules/log/event.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/modules/log/event.go b/modules/log/event.go index f66ecd179b701..40beefedbfa47 100644 --- a/modules/log/event.go +++ b/modules/log/event.go @@ -345,18 +345,41 @@ func (m *MultiChannelledLog) Start() { m.closeLoggers() return } + m.emptyQueue() m.rwmutex.RLock() for _, logger := range m.loggers { logger.Flush() } m.rwmutex.RUnlock() case <-m.close: + m.emptyQueue() m.closeLoggers() return } } } +func (m *MultiChannelledLog) emptyQueue() bool { + for { + select { + case event, ok := <-m.queue: + if !ok { + return false + } + m.rwmutex.RLock() + for _, logger := range m.loggers { + err := logger.LogEvent(event) + if err != nil { + fmt.Println(err) + } + } + m.rwmutex.RUnlock() + default: + return true + } + } +} + // LogEvent logs an event to this MultiChannelledLog func (m *MultiChannelledLog) LogEvent(event *Event) error { select { From 267062973ed673b74a3a07eb53a42880aa3ca39f Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 17 Jun 2022 22:51:47 +0100 Subject: [PATCH 2/2] and do similar for ChannelledLog Signed-off-by: Andrew Thornton --- modules/log/event.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/modules/log/event.go b/modules/log/event.go index 40beefedbfa47..41bb241da840a 100644 --- a/modules/log/event.go +++ b/modules/log/event.go @@ -89,8 +89,10 @@ func (l *ChannelledLog) Start() { l.closeLogger() return } + l.emptyQueue() l.loggerProvider.Flush() case <-l.close: + l.emptyQueue() l.closeLogger() return } @@ -111,6 +113,20 @@ func (l *ChannelledLog) LogEvent(event *Event) error { } } +func (l *ChannelledLog) emptyQueue() bool { + for { + select { + case event, ok := <-l.queue: + if !ok { + return false + } + l.loggerProvider.LogEvent(event) + default: + return true + } + } +} + func (l *ChannelledLog) closeLogger() { l.loggerProvider.Flush() l.loggerProvider.Close()