Skip to content

Commit

Permalink
fix(log-levels): do not ignore configuration file log levels (ChainSa…
Browse files Browse the repository at this point in the history
…fe#2016)

- Fix config log levels being ignored (remove default flag values)
- Use `parseLogLevelString` in other places to reduce code duplication
- Rename `cfg` to `tomlConfig` in `setLogConfig`
  • Loading branch information
qdm12 authored and timwu20 committed Dec 6, 2021
1 parent ddd96f7 commit 44c9b7a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
40 changes: 21 additions & 19 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,16 @@ func parseLogLevelString(logLevelString string) (logLevel log.Level, err error)
return logLevel, nil
}

func setLogConfig(flagsKVStore stringKVStore, cfg *ctoml.Config, globalCfg *dot.GlobalConfig, logCfg *dot.LogConfig) (err error) {
if cfg == nil {
cfg = new(ctoml.Config)
func setLogConfig(flagsKVStore stringKVStore, tomlConfig *ctoml.Config, globalCfg *dot.GlobalConfig, logCfg *dot.LogConfig) (err error) {
if tomlConfig == nil {
tomlConfig = new(ctoml.Config)
}

globalCfg.LogLvl, err = getLogLevel(flagsKVStore, LogFlag.Name, cfg.Global.LogLvl, gssmr.DefaultLvl)
globalCfg.LogLvl, err = getLogLevel(flagsKVStore, LogFlag.Name, tomlConfig.Global.LogLvl, gssmr.DefaultLvl)
if err != nil {
return fmt.Errorf("cannot get global log level: %w", err)
}
cfg.Global.LogLvl = globalCfg.LogLvl.String()
tomlConfig.Global.LogLvl = globalCfg.LogLvl.String()

levelsData := []struct {
name string
Expand All @@ -335,55 +335,55 @@ func setLogConfig(flagsKVStore stringKVStore, cfg *ctoml.Config, globalCfg *dot.
{
name: "core",
flagName: LogCoreLevelFlag.Name,
tomlValue: cfg.Log.CoreLvl,
tomlValue: tomlConfig.Log.CoreLvl,
levelPtr: &logCfg.CoreLvl,
},
{
name: "sync",
flagName: LogSyncLevelFlag.Name,
tomlValue: cfg.Log.SyncLvl,
tomlValue: tomlConfig.Log.SyncLvl,
levelPtr: &logCfg.SyncLvl,
},
{
name: "network",
flagName: LogNetworkLevelFlag.Name,
tomlValue: cfg.Log.NetworkLvl,
tomlValue: tomlConfig.Log.NetworkLvl,
levelPtr: &logCfg.NetworkLvl,
},
{
name: "RPC",
flagName: LogRPCLevelFlag.Name,
tomlValue: cfg.Log.RPCLvl,
tomlValue: tomlConfig.Log.RPCLvl,
levelPtr: &logCfg.RPCLvl,
},
{
name: "state",
flagName: LogStateLevelFlag.Name,
tomlValue: cfg.Log.StateLvl,
tomlValue: tomlConfig.Log.StateLvl,
levelPtr: &logCfg.StateLvl,
},
{
name: "runtime",
flagName: LogRuntimeLevelFlag.Name,
tomlValue: cfg.Log.RuntimeLvl,
tomlValue: tomlConfig.Log.RuntimeLvl,
levelPtr: &logCfg.RuntimeLvl,
},
{
name: "block producer",
flagName: LogBabeLevelFlag.Name,
tomlValue: cfg.Log.BlockProducerLvl,
tomlValue: tomlConfig.Log.BlockProducerLvl,
levelPtr: &logCfg.BlockProducerLvl,
},
{
name: "finality gadget",
flagName: LogGrandpaLevelFlag.Name,
tomlValue: cfg.Log.FinalityGadgetLvl,
tomlValue: tomlConfig.Log.FinalityGadgetLvl,
levelPtr: &logCfg.FinalityGadgetLvl,
},
{
name: "sync",
flagName: LogSyncLevelFlag.Name,
tomlValue: cfg.Log.SyncLvl,
tomlValue: tomlConfig.Log.SyncLvl,
levelPtr: &logCfg.SyncLvl,
},
}
Expand Down Expand Up @@ -442,7 +442,10 @@ func setDotGlobalConfigFromToml(tomlCfg *ctoml.Config, cfg *dot.GlobalConfig) {
}

if tomlCfg.Global.LogLvl != "" {
cfg.LogLvl, _ = log.ParseLevel(tomlCfg.Global.LogLvl)
level, err := parseLogLevelString(tomlCfg.Global.LogLvl)
if err == nil {
cfg.LogLvl = level
}
}

cfg.MetricsPort = tomlCfg.Global.MetricsPort
Expand All @@ -465,10 +468,9 @@ func setDotGlobalConfigFromFlags(ctx *cli.Context, cfg *dot.GlobalConfig) error
}

// check --log flag
if lvlToInt, err := strconv.Atoi(ctx.String(LogFlag.Name)); err == nil {
cfg.LogLvl = log.Level(lvlToInt)
} else if lvl, err := log.ParseLevel(ctx.String(LogFlag.Name)); err == nil {
cfg.LogLvl = lvl
logLevel, err := parseLogLevelString(ctx.String(LogFlag.Name))
if err == nil {
cfg.LogLvl = logLevel
}

cfg.PublishMetrics = ctx.Bool("publish-metrics")
Expand Down
10 changes: 0 additions & 10 deletions cmd/gossamer/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main

import (
"github.com/ChainSafe/gossamer/chain/dev"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -44,47 +43,38 @@ var (
LogFlag = cli.StringFlag{
Name: "log",
Usage: "Global log level. Supports levels crit (silent), eror, warn, info, dbug and trce (trace)",
Value: log.Info.String(),
}
LogCoreLevelFlag = cli.StringFlag{
Name: "log-core",
Usage: "Core package log level. Supports levels crit (silent), eror, warn, info, dbug and trce (trace)",
Value: LogFlag.Value,
}
LogSyncLevelFlag = cli.StringFlag{
Name: "log-sync",
Usage: "Sync package log level. Supports levels crit (silent), eror, warn, info, dbug and trce (trace)",
Value: LogFlag.Value,
}
LogNetworkLevelFlag = cli.StringFlag{
Name: "log-network",
Usage: "Network package log level. Supports levels crit (silent), eror, warn, info, dbug and trce (trace)",
Value: LogFlag.Value,
}
LogRPCLevelFlag = cli.StringFlag{
Name: "log-rpc",
Usage: "RPC package log level. Supports levels crit (silent), eror, warn, info, dbug and trce (trace)",
Value: LogFlag.Value,
}
LogStateLevelFlag = cli.StringFlag{
Name: "log-state",
Usage: "State package log level. Supports levels crit (silent), eror, warn, info, dbug and trce (trace)",
Value: LogFlag.Value,
}
LogRuntimeLevelFlag = cli.StringFlag{
Name: "log-runtime",
Usage: "Runtime package log level. Supports levels crit (silent), eror, warn, info, dbug and trce (trace)",
Value: LogFlag.Value,
}
LogBabeLevelFlag = cli.StringFlag{
Name: "log-babe",
Usage: "BABE package log level. Supports levels crit (silent), eror, warn, info, dbug and trce (trace)",
Value: LogFlag.Value,
}
LogGrandpaLevelFlag = cli.StringFlag{
Name: "log-grandpa",
Usage: "Grandpa package log level. Supports levels crit (silent), eror, warn, info, dbug and trce (trace)",
Value: LogFlag.Value,
}

// NameFlag node implementation name
Expand Down
8 changes: 3 additions & 5 deletions cmd/gossamer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"syscall"
"testing"
Expand All @@ -24,10 +23,9 @@ const confirmCharacter = "Y"

// setupLogger sets up the global Gossamer logger.
func setupLogger(ctx *cli.Context) (level log.Level, err error) {
if lvlToInt, err := strconv.Atoi(ctx.String(LogFlag.Name)); err == nil {
level = log.Level(lvlToInt)
} else if level, err = log.ParseLevel(ctx.String(LogFlag.Name)); err != nil {
return 0, err
level, err = getLogLevel(ctx, LogFlag.Name, "", log.Info)
if err != nil {
return level, err
}

log.Patch(
Expand Down
2 changes: 1 addition & 1 deletion cmd/gossamer/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func Test_setupLogger(t *testing.T) {
"Test gossamer --log blah",
[]string{"log"},
[]interface{}{"blah"},
errors.New("level is not recognised: blah"),
errors.New("cannot parse log level string: level is not recognised: blah"),
},
}

Expand Down

0 comments on commit 44c9b7a

Please sign in to comment.