Skip to content

Commit 31eff16

Browse files
committed
logging: ForceLog option to log all messages
Once a zap logger is created, its minimum log level is set in its core. It is possible to log at this level or at a higher level, for example, when setting info, one can log at info or error. However, it is not possible to decrease the level of the logger. This can be useful when the user has configured a high default level, but some messages still need to be logged without giving them a ridiculously high level, e.g., a start log entry with a version string as an error log entry.
1 parent c2f0318 commit 31eff16

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

logging/logging.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,14 @@ func (l *Logging) GetChildLogger(name string) *Logger {
117117
func (l *Logging) GetLogger() *Logger {
118118
return l.logger
119119
}
120+
121+
// ForceLog results in every message being logged.
122+
//
123+
// This [zap.Option] is the opposite of [zap.IncreaseLevel], it just decreases the log level to debug. Since zap's
124+
// architecture does not allow this with the same [zapcore.Core], it replaces the core with a freshly created one from
125+
// the Logging's core factory.
126+
func (l *Logging) ForceLog() zap.Option {
127+
return zap.WrapCore(func(_ zapcore.Core) zapcore.Core {
128+
return l.coreFactory(zap.NewAtomicLevelAt(zapcore.DebugLevel))
129+
})
130+
}

0 commit comments

Comments
 (0)