Browse Source

logging: allow logging level override (#7873)

I think in the future we should migrate from having our own logging
interface and use our logger directly, which I think would help
obviate this particular problem, but in the mean time, this seems safe.
pull/7890/head
Sam Kleinman 3 years ago
committed by GitHub
parent
commit
d65237ff87
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions
  1. +4
    -0
      cmd/tendermint/commands/root.go
  2. +25
    -5
      libs/log/default.go
  3. +1
    -1
      libs/log/nop.go

+ 4
- 0
cmd/tendermint/commands/root.go View File

@ -52,6 +52,10 @@ func RootCommand(conf *config.Config, logger log.Logger) *cobra.Command {
*conf = *pconf *conf = *pconf
config.EnsureRoot(conf.RootDir) config.EnsureRoot(conf.RootDir)
if err := log.OverrideWithNewLogger(logger, conf.LogFormat, conf.LogLevel); err != nil {
return err
}
return nil return nil
}, },
} }


+ 25
- 5
libs/log/default.go View File

@ -55,7 +55,7 @@ func NewDefaultLogger(format, level string) (Logger, error) {
// make the writer thread-safe // make the writer thread-safe
logWriter = newSyncWriter(logWriter) logWriter = newSyncWriter(logWriter)
return defaultLogger{
return &defaultLogger{
Logger: zerolog.New(logWriter).Level(logLevel).With().Timestamp().Logger(), Logger: zerolog.New(logWriter).Level(logLevel).With().Timestamp().Logger(),
}, nil }, nil
} }
@ -76,9 +76,7 @@ func (l defaultLogger) Info(msg string, keyVals ...interface{}) {
} }
func (l defaultLogger) Error(msg string, keyVals ...interface{}) { func (l defaultLogger) Error(msg string, keyVals ...interface{}) {
e := l.Logger.Error()
e.Fields(getLogFields(keyVals...)).Msg(msg)
l.Logger.Error().Fields(getLogFields(keyVals...)).Msg(msg)
} }
func (l defaultLogger) Debug(msg string, keyVals ...interface{}) { func (l defaultLogger) Debug(msg string, keyVals ...interface{}) {
@ -86,11 +84,33 @@ func (l defaultLogger) Debug(msg string, keyVals ...interface{}) {
} }
func (l defaultLogger) With(keyVals ...interface{}) Logger { func (l defaultLogger) With(keyVals ...interface{}) Logger {
return defaultLogger{
return &defaultLogger{
Logger: l.Logger.With().Fields(getLogFields(keyVals...)).Logger(), Logger: l.Logger.With().Fields(getLogFields(keyVals...)).Logger(),
} }
} }
// OverrideWithNewLogger replaces an existing logger's internal with
// a new logger, and makes it possible to reconfigure an existing
// logger that has already been propagated to callers.
func OverrideWithNewLogger(logger Logger, format, level string) error {
ol, ok := logger.(*defaultLogger)
if !ok {
return fmt.Errorf("logger %T cannot be overridden", logger)
}
newLogger, err := NewDefaultLogger(format, level)
if err != nil {
return err
}
nl, ok := newLogger.(*defaultLogger)
if !ok {
return fmt.Errorf("logger %T cannot be overridden by %T", logger, newLogger)
}
ol.Logger = nl.Logger
return nil
}
func getLogFields(keyVals ...interface{}) map[string]interface{} { func getLogFields(keyVals ...interface{}) map[string]interface{} {
if len(keyVals)%2 != 0 { if len(keyVals)%2 != 0 {
return nil return nil


+ 1
- 1
libs/log/nop.go View File

@ -5,7 +5,7 @@ import (
) )
func NewNopLogger() Logger { func NewNopLogger() Logger {
return defaultLogger{
return &defaultLogger{
Logger: zerolog.Nop(), Logger: zerolog.Nop(),
} }
} }

Loading…
Cancel
Save