Skip to content

Commit

Permalink
feat(cli): use a single flag for log level in the CLI (#3303)
Browse files Browse the repository at this point in the history
added a log flag to set the log level of modules.
  • Loading branch information
axaysagathiya authored Jun 23, 2023
1 parent 8d76664 commit caf3ea4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 100 deletions.
8 changes: 7 additions & 1 deletion cmd/gossamer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ Here are the list of basic flags for the `gossamer` command:
--key: The keypair to use for the node.
--name: The name of the node.
--id: The id of the node.
--log-level: The global log level. Supported levels are `crit`, `error`, `warn`, `info`, `debug` and `trace`.
--log: Set a logging filter.
Syntax is a list of 'module=logLevel' (comma separated)
e.g. --log sync=debug,core=trace
Modules are global, core, digest, sync, network, rpc, state, runtime, babe, grandpa, wasmer.
Log levels (least to most verbose) are error, warn, info, debug, and trace.
By default, all modules log 'info'.
The global log level can be set with --log global=debug
--prometheus-port: The port to expose prometheus metrics.
--retain-blocks: retain number of block from latest block while pruning
--pruning: The pruning strategy to use. Supported strategiey: `archive`
Expand Down
103 changes: 16 additions & 87 deletions cmd/gossamer/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ var (

// Flag values for the root command which needs type conversion
var (
logLevel string

// Base Config
logLevelGlobal string
pruning string
telemetryURLs string
pruning string
telemetryURLs string

// Core Config
// role of the node. one of: full, light or authority
Expand Down Expand Up @@ -117,6 +118,10 @@ Usage:
}
}

if err := parseLogLevel(); err != nil {
return fmt.Errorf("failed to parse log level: %s", err)
}

if cmd.Name() == "gossamer" {
if err := configureViper(config.BasePath); err != nil {
return fmt.Errorf("failed to configure viper: %s", err)
Expand Down Expand Up @@ -162,9 +167,14 @@ func addRootFlags(cmd *cobra.Command) error {
}

// Log Config
if err := addLogFlags(cmd); err != nil {
return fmt.Errorf("failed to add log flags: %s", err)
}
cmd.PersistentFlags().StringVarP(&logLevel, "log", "l", "",
`Set a logging filter.
Syntax is a list of 'module=logLevel' (comma separated)
e.g. --log sync=debug,core=trace
Modules are global, core, digest, sync, network, rpc, state, runtime, babe, grandpa, wasmer.
Log levels (least to most verbose) are error, warn, info, debug, and trace.
By default, all modules log 'info'.
The global log level can be set with --log global=debug`)

// Account Config
if err := addAccountFlags(cmd); err != nil {
Expand Down Expand Up @@ -233,10 +243,6 @@ func addBaseConfigFlags(cmd *cobra.Command) error {
"retain-blocks"); err != nil {
return fmt.Errorf("failed to add --retain-blocks flag: %s", err)
}
cmd.Flags().StringVar(&logLevelGlobal,
"log",
config.BaseConfig.LogLevel,
"Global log level. Supports levels critical (silent), error, warn, info, debug and trace")
cmd.Flags().StringVar(&pruning,
"state-pruning",
string(config.BaseConfig.Pruning),
Expand All @@ -260,83 +266,6 @@ Expected format is 'URL VERBOSITY', e.g. ''--telemetry-url wss://foo/bar:0, wss:
return nil
}

// addLogFlags adds the log flags to the command
func addLogFlags(cmd *cobra.Command) error {
if err := addStringFlagBindViper(cmd,
"lcore",
config.Log.Core,
"Core module log level",
"log.core"); err != nil {
return fmt.Errorf("failed to add --lcore flag: %s", err)
}

if err := addStringFlagBindViper(cmd,
"ldigest",
config.Log.Digest,
"Digest module log level",
"log.digest"); err != nil {
return fmt.Errorf("failed to add --ldigest flag: %s", err)
}

if err := addStringFlagBindViper(cmd,
"lsync",
config.Log.Sync,
"Sync module log level",
"log.sync"); err != nil {
return fmt.Errorf("failed to add --lsync flag: %s", err)
}

if err := addStringFlagBindViper(cmd,
"lnetwork",
config.Log.Network,
"Network module log level",
"log.network"); err != nil {
return fmt.Errorf("failed to add --lnetwork flag: %s", err)
}

if err := addStringFlagBindViper(cmd,
"lrpc",
config.Log.RPC,
"RPC module log level",
"log.rpc"); err != nil {
return fmt.Errorf("failed to add --lrpc flag: %s", err)
}

if err := addStringFlagBindViper(cmd,
"lstate",
config.Log.State,
"State module log level",
"log.state"); err != nil {
return fmt.Errorf("failed to add --lstate flag: %s", err)
}

if err := addStringFlagBindViper(cmd,
"lruntime",
config.Log.Runtime,
"Runtime module log level",
"log.runtime"); err != nil {
return fmt.Errorf("failed to add --lruntime flag: %s", err)
}

if err := addStringFlagBindViper(cmd,
"lbabe",
config.Log.Babe,
"BABE module log level",
"log.babe"); err != nil {
return fmt.Errorf("failed to add --lbabe flag: %s", err)
}

if err := addStringFlagBindViper(cmd,
"lgrandpa",
config.Log.Grandpa,
"GRANDPA module log level",
"log.grandpa"); err != nil {
return fmt.Errorf("failed to add --lgrandpa flag: %s", err)
}

return nil
}

// addAccountFlags adds account flags and binds to viper
func addAccountFlags(cmd *cobra.Command) error {
cmd.PersistentFlags().StringVar(&key,
Expand Down
53 changes: 53 additions & 0 deletions cmd/gossamer/commands/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package commands

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -481,3 +482,55 @@ func setViperDefault(config *cfg.Config) {
}
}
}

func parseLogLevel() error {
// set default log level
moduleToLogLevel := map[string]string{
"global": cfg.DefaultLogLevel,
"core": cfg.DefaultLogLevel,
"digest": cfg.DefaultLogLevel,
"sync": cfg.DefaultLogLevel,
"network": cfg.DefaultLogLevel,
"rpc": cfg.DefaultLogLevel,
"state": cfg.DefaultLogLevel,
"runtime": cfg.DefaultLogLevel,
"babe": cfg.DefaultLogLevel,
"grandpa": cfg.DefaultLogLevel,
"wasmer": cfg.DefaultLogLevel,
}

if logLevel != "" {
logConfigurations := strings.Split(logLevel, ",")
for _, config := range logConfigurations {
parts := strings.SplitN(config, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("invalid log configuration: %s", config)
}

module := strings.TrimSpace(parts[0])
logLevel := strings.TrimSpace(parts[1])

if _, ok := moduleToLogLevel[module]; !ok {
return fmt.Errorf("invalid module: %s", module)
}
moduleToLogLevel[module] = logLevel
}
}

// set global log level
config.LogLevel = moduleToLogLevel["global"]
viper.Set("log-level", config.LogLevel)

// set config.Log
jsonData, err := json.Marshal(moduleToLogLevel)
if err != nil {
return fmt.Errorf("error marshalling logs: %s", err)
}
err = json.Unmarshal(jsonData, &config.Log)
if err != nil {
return fmt.Errorf("error unmarshalling logs: %s", err)
}
viper.Set("log", config.Log)

return nil
}
4 changes: 2 additions & 2 deletions docs/docs/testing-and-debugging/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ grandpa = "trace | debug | info | warn | error | crit"
```

## Logging Global Flags
```--log value Supports levels crit (silent) to trce (trace) (default: "info")```
```--log global=value Supports levels crit (silent) to trce (trace) (default: "info")```

## Running node with log level as `DEBUG`
```./bin/gossamer --config chain/gssmr/config.toml --log debug```
```./bin/gossamer --config chain/gssmr/config.toml --log global=debug```
17 changes: 7 additions & 10 deletions docs/docs/usage/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ These are the flags that can be used with the `gossamer` command
--help help for gossamer
--id Identifier used to identify this node in the network
--key Key to use for the node
--lbabe Log level for BABE module (default 'info')
--lcore Log level for core module (default 'info')
--ldigest Log level for digest module (default 'info')
--lgrandpa Log level for GRANDPA module (default 'info')
--listen-addr Overrides the listen address used for peer to peer networking
--lnetwork Log level for network module (default 'info')
--log Global log level (default 'info')
--lrpc Log level for RPC module (default 'info')
--lruntime Log level for runtime module (default 'info')
--lstate Log level for state module (default 'info')
--lsync Log level for sync module (default 'info')
--log: Set a logging filter.
Syntax is a list of 'module=logLevel' (comma separated)
e.g. --log sync=debug,core=trace
Modules are global, core, digest, sync, network, rpc, state, runtime, babe, grandpa, wasmer.
Log levels (least to most verbose) are error, warn, info, debug, and trace.
By default, all modules log 'info'.
The global log level can be set with --log global=debug
--max-peers Maximum number of peers to connect to (default 50)
--min-peers Minimum number of peers to connect to (default 5)
--name Name of the node
Expand Down

0 comments on commit caf3ea4

Please sign in to comment.