From f936c7d317de6be0474fbd78540b806b5f2b96b7 Mon Sep 17 00:00:00 2001 From: Sujong Lee Date: Thu, 9 Feb 2023 12:59:04 +0900 Subject: [PATCH] feat: add zerolog based rolling log system (#883) * feat: add zerolog based rolling log system * test: init log level when ostracon config isn't created * docs: add changelog * fix: log_level command help --- CHANGELOG.md | 1 + client/flags/flags.go | 8 ++++-- go.mod | 2 +- server/cmd/execute.go | 3 +-- server/logger.go | 59 ------------------------------------------- server/util.go | 34 ++++++++++++++----------- 6 files changed, 28 insertions(+), 79 deletions(-) delete mode 100644 server/logger.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 056899846a..4c3956f570 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (baseapp) [\#840](https://github.com/line/lbm-sdk/pull/840) allow querying the state based on `CheckState`. * (x/foundation) [\#848](https://github.com/line/lbm-sdk/pull/848) remove `gov mint` for x/foundation proposal * (x/wasm) [\#850](https://github.com/line/lbm-sdk/pull/850) remove `x/wasm` module in lbm-sdk +* (log) [\#883](https://github.com/line/lbm-sdk/pull/883) add zerolog based rolling log system ### Improvements * (cosmovisor) [\#792](https://github.com/line/lbm-sdk/pull/792) Use upstream's cosmovisor diff --git a/client/flags/flags.go b/client/flags/flags.go index 884c3d5721..77b57de984 100644 --- a/client/flags/flags.go +++ b/client/flags/flags.go @@ -77,8 +77,12 @@ const ( FlagReverse = "reverse" // Tendermint logging flags - FlagLogLevel = "log_level" - FlagLogFormat = "log_format" + FlagLogLevel = "log_level" + FlagLogFormat = "log_format" + FlagLogPath = "log_path" + FlagLogMaxAge = "log_max_age" + FlagLogMaxSize = "log_max_size" + FlagLogMaxBackups = "log_max_backups" ) // LineBreak can be included in a command list to provide a blank line diff --git a/go.mod b/go.mod index 4119bf6dd8..5d5a2bbf91 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,6 @@ require ( github.com/prometheus/common v0.39.0 github.com/rakyll/statik v0.1.7 github.com/regen-network/cosmos-proto v0.3.1 - github.com/rs/zerolog v1.29.0 github.com/spf13/cast v1.5.0 github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 @@ -113,6 +112,7 @@ require ( github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect github.com/rogpeppe/go-internal v1.9.0 // indirect github.com/rs/cors v1.8.3 // indirect + github.com/rs/zerolog v1.29.0 // indirect github.com/sasha-s/go-deadlock v0.3.1 // indirect github.com/spf13/afero v1.9.3 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/server/cmd/execute.go b/server/cmd/execute.go index d429bf163b..ee5438826a 100644 --- a/server/cmd/execute.go +++ b/server/cmd/execute.go @@ -5,7 +5,6 @@ import ( ostcfg "github.com/line/ostracon/config" ostcli "github.com/line/ostracon/libs/cli" - "github.com/rs/zerolog" "github.com/spf13/cobra" "github.com/line/lbm-sdk/client" @@ -33,7 +32,7 @@ func Execute(rootCmd *cobra.Command, defaultHome string) error { ctx = context.WithValue(ctx, client.ClientContextKey, &client.Context{}) ctx = context.WithValue(ctx, server.ServerContextKey, srvCtx) - rootCmd.PersistentFlags().String(flags.FlagLogLevel, zerolog.InfoLevel.String(), "The logging level (trace|debug|info|warn|error|fatal|panic)") + rootCmd.PersistentFlags().String(flags.FlagLogLevel, ostcfg.DefaultPackageLogLevels(), "The logging level by modules (debug|info|error|none)") rootCmd.PersistentFlags().String(flags.FlagLogFormat, ostcfg.LogFormatPlain, "The logging format (json|plain)") executor := ostcli.PrepareBaseCmd(rootCmd, envPrefix, defaultHome) diff --git a/server/logger.go b/server/logger.go deleted file mode 100644 index e77746eff4..0000000000 --- a/server/logger.go +++ /dev/null @@ -1,59 +0,0 @@ -package server - -import ( - ostlog "github.com/line/ostracon/libs/log" - "github.com/rs/zerolog" -) - -var _ ostlog.Logger = (*ZeroLogWrapper)(nil) - -// ZeroLogWrapper provides a wrapper around a zerolog.Logger instance. It implements -// Tendermint's Logger interface. -type ZeroLogWrapper struct { - zerolog.Logger -} - -func init() { - zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs -} - -// 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{}) ostlog.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 -} diff --git a/server/util.go b/server/util.go index fe7e5e6b31..0e64c5f28b 100644 --- a/server/util.go +++ b/server/util.go @@ -17,8 +17,6 @@ import ( ostcmd "github.com/line/ostracon/cmd/ostracon/commands" ostcfg "github.com/line/ostracon/config" ostlog "github.com/line/ostracon/libs/log" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -57,7 +55,7 @@ func NewDefaultContext() *Context { return NewContext( viper.New(), ostcfg.DefaultConfig(), - ZeroLogWrapper{log.Logger}, + ostlog.ZeroLogWrapper{}, ) } @@ -142,23 +140,29 @@ func InterceptConfigsPreRunHandler(cmd *cobra.Command, customAppConfigTemplate s return err } - var logWriter io.Writer + isLogPlain := false if strings.ToLower(serverCtx.Viper.GetString(flags.FlagLogFormat)) == ostcfg.LogFormatPlain { - logWriter = zerolog.ConsoleWriter{ - Out: os.Stderr, - TimeFormat: "2006/01/02-15:04:05.999", - } - } else { - logWriter = os.Stderr + isLogPlain = true } - logLvlStr := serverCtx.Viper.GetString(flags.FlagLogLevel) - logLvl, err := zerolog.ParseLevel(logLvlStr) - if err != nil { - return fmt.Errorf("failed to parse log level (%s): %w", logLvlStr, err) + logLevel := serverCtx.Viper.GetString(flags.FlagLogLevel) + if logLevel == "" { + logLevel = ostcfg.DefaultPackageLogLevels() } - serverCtx.Logger = ZeroLogWrapper{zerolog.New(logWriter).Level(logLvl).With().Timestamp().Logger()} + zerologCfg := ostlog.NewZeroLogConfig( + isLogPlain, + logLevel, + serverCtx.Viper.GetString(flags.FlagLogPath), + serverCtx.Viper.GetInt(flags.FlagLogMaxAge), + serverCtx.Viper.GetInt(flags.FlagLogMaxSize), + serverCtx.Viper.GetInt(flags.FlagLogMaxBackups), + ) + + serverCtx.Logger, err = ostlog.NewZeroLogLogger(zerologCfg, os.Stderr) + if err != nil { + return fmt.Errorf("failed to initialize logger: %w", err) + } return SetCmdServerContext(cmd, serverCtx) }