1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-22 16:44:26 +00:00

Empty log queue on flush and close (#19994)

* 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 <art27@cantab.net>

* and do similar for ChannelledLog

Signed-off-by: Andrew Thornton <art27@cantab.net>

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
zeripath 2022-06-18 03:33:13 +01:00 committed by GitHub
parent 46e50bcbfc
commit 8eefe2af45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -89,8 +89,10 @@ func (l *ChannelledLog) Start() {
l.closeLogger() l.closeLogger()
return return
} }
l.emptyQueue()
l.loggerProvider.Flush() l.loggerProvider.Flush()
case <-l.close: case <-l.close:
l.emptyQueue()
l.closeLogger() l.closeLogger()
return 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() { func (l *ChannelledLog) closeLogger() {
l.loggerProvider.Flush() l.loggerProvider.Flush()
l.loggerProvider.Close() l.loggerProvider.Close()
@ -345,18 +361,41 @@ func (m *MultiChannelledLog) Start() {
m.closeLoggers() m.closeLoggers()
return return
} }
m.emptyQueue()
m.rwmutex.RLock() m.rwmutex.RLock()
for _, logger := range m.loggers { for _, logger := range m.loggers {
logger.Flush() logger.Flush()
} }
m.rwmutex.RUnlock() m.rwmutex.RUnlock()
case <-m.close: case <-m.close:
m.emptyQueue()
m.closeLoggers() m.closeLoggers()
return 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 // LogEvent logs an event to this MultiChannelledLog
func (m *MultiChannelledLog) LogEvent(event *Event) error { func (m *MultiChannelledLog) LogEvent(event *Event) error {
select { select {