cmd/cli: set ProxyLogger correctly for interactive commands

The ProxyLogger must only be set after mainLog is fully initialized.
However, it's being set before the final initialization of mainlog,
causing it still refers to stale old pointer.

To fix this, introduce a new function to discard ProxyLogger explicitly,
and use this function to init logging for all interactive commands.
This commit is contained in:
Cuong Manh Le authored and Cuong Manh Le committed 2025-02-05 23:39:49 +07:00
1 parent 47d7ace3a7
commit 60686f55ff
3 files changed
+15 -7

No files matched your search

+10
View File
@@ -106,6 +106,14 @@ func initLogging() []io.Writer {
return initLoggingWithBackup(true)
}
// initInteractiveLogging is like initLogging, but the ProxyLogger is discarded
// to be used for all interactive commands.
func initInteractiveLogging() {
initLogging()
l := zerolog.New(io.Discard)
ctrld.ProxyLogger.Store(&l)
}
// initLoggingWithBackup initializes log setup base on current config.
// If doBackup is true, backup old log file with ".1" suffix.
//
@@ -143,6 +151,8 @@ func initLoggingWithBackup(doBackup bool) []io.Writer {
multi := zerolog.MultiLevelWriter(writers...)
l := mainLog.Load().Output(multi).With().Logger()
mainLog.Store(&l)
// TODO: find a better way.
ctrld.ProxyLogger.Store(&l)
zerolog.SetGlobalLevel(zerolog.NoticeLevel)
logLevel := cfg.Service.LogLevel