logs: flush Logger through klog

Not all code knows that it needs to flush through component-base/logs.FlushLogs
when the JSON logger is used. By registering the flush callback together with
the logger, klog.Flush and klog.FlushAndExit are sufficient for flushing all
data.
This commit is contained in:
Patrick Ohly 2022-03-16 12:12:46 +01:00
parent 09aa1071cd
commit f8bb67cc2e
2 changed files with 1 additions and 6 deletions

View File

@ -41,7 +41,6 @@ const deprecated = "will be removed in a future release, see https://github.com/
var (
packageFlags = flag.NewFlagSet("logging", flag.ContinueOnError)
logrFlush func()
// Periodic flushing gets configured either via the global flag
// in this file or via LoggingConfiguration.
@ -185,9 +184,6 @@ func InitLogs() {
// are printed before exiting the program.
func FlushLogs() {
klog.Flush()
if logrFlush != nil {
logrFlush()
}
}
// NewLogger creates a new log.Logger which sends logs to klog.Info.

View File

@ -82,8 +82,7 @@ func (o *Options) apply() {
klog.ClearLogger()
} else {
log, flush := factory.Create(o.Config.Options)
klog.SetLogger(log)
logrFlush = flush
klog.SetLoggerWithOptions(log, klog.FlushLogger(flush))
}
if err := loggingFlags.Lookup("v").Value.Set(o.Config.Verbosity.String()); err != nil {
panic(fmt.Errorf("internal error while setting klog verbosity: %v", err))