Skip to content

Commit

Permalink
feat: add zerolog based rolling log system (#883)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dudong2 authored Feb 9, 2023
1 parent ec7cd38 commit f936c7d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 79 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions server/cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
59 changes: 0 additions & 59 deletions server/logger.go

This file was deleted.

34 changes: 19 additions & 15 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -57,7 +55,7 @@ func NewDefaultContext() *Context {
return NewContext(
viper.New(),
ostcfg.DefaultConfig(),
ZeroLogWrapper{log.Logger},
ostlog.ZeroLogWrapper{},
)
}

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit f936c7d

Please sign in to comment.