Refactor Logging using Zerolog (#8072)
* init commit * server: use flags * server: godoc++ * updates * baseapp: update logging * logging updates * x/bank: update logging * logging updates * lint++ * logging updates * logging updates * logging updates * logging updates * cl++
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
tmlog "github.com/tendermint/tendermint/libs/log"
|
||||
)
|
||||
|
||||
var _ tmlog.Logger = (*ZeroLogWrapper)(nil)
|
||||
|
||||
// ZeroLogWrapper provides a wrapper around a zerolog.Logger instance. It implements
|
||||
// Tendermint's Logger interface.
|
||||
type ZeroLogWrapper struct {
|
||||
zerolog.Logger
|
||||
}
|
||||
|
||||
// Info implements Tendermint's Logger interface and logs with level INFO. A set
|
||||
// of key/value tuples may be provided to add context to the log. The number of
|
||||
// tuples must be even and the key of the tuple must be a string.
|
||||
func (z ZeroLogWrapper) Info(msg string, keyVals ...interface{}) {
|
||||
z.Logger.Info().Fields(getLogFields(keyVals...)).Msg(msg)
|
||||
}
|
||||
|
||||
// Error implements Tendermint's Logger interface and logs with level ERR. A set
|
||||
// of key/value tuples may be provided to add context to the log. The number of
|
||||
// tuples must be even and the key of the tuple must be a string.
|
||||
func (z ZeroLogWrapper) Error(msg string, keyVals ...interface{}) {
|
||||
z.Logger.Error().Fields(getLogFields(keyVals...)).Msg(msg)
|
||||
}
|
||||
|
||||
// Debug implements Tendermint's Logger interface and logs with level DEBUG. A set
|
||||
// of key/value tuples may be provided to add context to the log. The number of
|
||||
// tuples must be even and the key of the tuple must be a string.
|
||||
func (z ZeroLogWrapper) Debug(msg string, keyVals ...interface{}) {
|
||||
z.Logger.Debug().Fields(getLogFields(keyVals...)).Msg(msg)
|
||||
}
|
||||
|
||||
// With returns a new wrapped logger with additional context provided by a set
|
||||
// of key/value tuples. The number of tuples must be even and the key of the
|
||||
// tuple must be a string.
|
||||
func (z ZeroLogWrapper) With(keyVals ...interface{}) tmlog.Logger {
|
||||
return ZeroLogWrapper{z.Logger.With().Fields(getLogFields(keyVals...)).Logger()}
|
||||
}
|
||||
|
||||
func getLogFields(keyVals ...interface{}) map[string]interface{} {
|
||||
if len(keyVals)%2 != 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
fields := make(map[string]interface{})
|
||||
for i := 0; i < len(keyVals); i += 2 {
|
||||
fields[keyVals[i].(string)] = keyVals[i+1]
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
+3
-3
@@ -240,17 +240,17 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
|
||||
genDocProvider,
|
||||
node.DefaultDBProvider,
|
||||
node.DefaultMetricsProvider(cfg.Instrumentation),
|
||||
ctx.Logger.With("module", "node"),
|
||||
ctx.Logger,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.Logger.Debug("Initialization: tmNode created")
|
||||
|
||||
ctx.Logger.Debug("initialization: tmNode created")
|
||||
if err := tmNode.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.Logger.Debug("Initialization: tmNode started")
|
||||
ctx.Logger.Debug("initialization: tmNode started")
|
||||
|
||||
config := config.GetConfig(ctx.Viper)
|
||||
|
||||
|
||||
+24
-18
@@ -14,12 +14,12 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
tmcfg "github.com/tendermint/tendermint/config"
|
||||
tmcli "github.com/tendermint/tendermint/libs/cli"
|
||||
tmflags "github.com/tendermint/tendermint/libs/cli/flags"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
tmlog "github.com/tendermint/tendermint/libs/log"
|
||||
dbm "github.com/tendermint/tm-db"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
@@ -39,7 +39,7 @@ const ServerContextKey = sdk.ContextKey("server.context")
|
||||
type Context struct {
|
||||
Viper *viper.Viper
|
||||
Config *tmcfg.Config
|
||||
Logger log.Logger
|
||||
Logger tmlog.Logger
|
||||
}
|
||||
|
||||
// ErrorCode contains the exit code for server exit.
|
||||
@@ -52,10 +52,14 @@ func (e ErrorCode) Error() string {
|
||||
}
|
||||
|
||||
func NewDefaultContext() *Context {
|
||||
return NewContext(viper.New(), tmcfg.DefaultConfig(), log.NewTMLogger(log.NewSyncWriter(os.Stdout)))
|
||||
return NewContext(
|
||||
viper.New(),
|
||||
tmcfg.DefaultConfig(),
|
||||
ZeroLogWrapper{log.Logger},
|
||||
)
|
||||
}
|
||||
|
||||
func NewContext(v *viper.Viper, config *tmcfg.Config, logger log.Logger) *Context {
|
||||
func NewContext(v *viper.Viper, config *tmcfg.Config, logger tmlog.Logger) *Context {
|
||||
return &Context{v, config, logger}
|
||||
}
|
||||
|
||||
@@ -86,27 +90,29 @@ func InterceptConfigsPreRunHandler(cmd *cobra.Command) error {
|
||||
serverCtx.Viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
|
||||
serverCtx.Viper.AutomaticEnv()
|
||||
|
||||
// Intercept configuration files, using both Viper instances separately
|
||||
// intercept configuration files, using both Viper instances separately
|
||||
config, err := interceptConfigs(serverCtx.Viper)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Return value is a tendermint configuration object
|
||||
|
||||
// return value is a tendermint configuration object
|
||||
serverCtx.Config = config
|
||||
|
||||
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
|
||||
logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, tmcfg.DefaultLogLevel())
|
||||
var logWriter io.Writer
|
||||
if strings.ToLower(serverCtx.Viper.GetString(flags.FlagLogFormat)) == tmcfg.LogFormatPlain {
|
||||
logWriter = zerolog.ConsoleWriter{Out: os.Stderr}
|
||||
} else {
|
||||
logWriter = os.Stderr
|
||||
}
|
||||
|
||||
logLvlStr := serverCtx.Viper.GetString(flags.FlagLogLevel)
|
||||
logLvl, err := zerolog.ParseLevel(logLvlStr)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to parse log level (%s): %w", logLvlStr, err)
|
||||
}
|
||||
|
||||
// Check if the tendermint flag for trace logging is set
|
||||
// if it is then setup a tracing logger in this app as well
|
||||
if serverCtx.Viper.GetBool(tmcli.TraceFlag) {
|
||||
logger = log.NewTracingLogger(logger)
|
||||
}
|
||||
|
||||
serverCtx.Logger = logger.With("module", "main")
|
||||
serverCtx.Logger = ZeroLogWrapper{zerolog.New(logWriter).Level(logLvl).With().Timestamp().Logger()}
|
||||
|
||||
return SetCmdServerContext(cmd, serverCtx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user