Skip to content

Commit

Permalink
feat(cmd/node): add logs cmd to dynamically change the log level (#2601)
Browse files Browse the repository at this point in the history
## Overview
Added cmds that allow to change log level during runtime

## Checklist

<!-- 
Please complete the checklist to ensure that the PR is ready to be
reviewed.

IMPORTANT:
PRs should be left in Draft until the below checklist is completed.
-->

- [ ] New and updated code has appropriate documentation
- [ ] New and updated code has new and/or updated testing
- [ ] Required CI checks are passing
- [ ] Visual proof for any user facing features like CLI or
documentation updates
- [ ] Linked issues closed with keywords
  • Loading branch information
vgonkivs authored Aug 23, 2023
1 parent b6efbc9 commit bbe970f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
48 changes: 48 additions & 0 deletions cmd/celestia/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/celestiaorg/celestia-node/cmd"
)

var logCmd = &cobra.Command{
Use: cmd.LogLevelFlag,
Args: cobra.ExactArgs(1),
Short: "Allows to set log level for all modules to " +
"`DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL and their lower-case forms`",

RunE: func(c *cobra.Command, args []string) error {
client, err := rpcClient(c.Context())
if err != nil {
return err
}
return client.Node.LogLevelSet(c.Context(), "*", args[0])
},
}

var logModuleCmd = &cobra.Command{
Use: cmd.LogLevelModuleFlag,
Args: cobra.MinimumNArgs(1),
Short: "Allows to set log level for a particular module in format <module>:<level>",
RunE: func(c *cobra.Command, args []string) error {
client, err := rpcClient(c.Context())
if err != nil {
return err
}
for _, ll := range args {
params := strings.Split(ll, ":")
if len(params) != 2 {
return fmt.Errorf("cmd: %s arg must be in form <module>:<level>,"+
"e.g. pubsub:debug", cmd.LogLevelModuleFlag)
}
if err = client.Node.LogLevelSet(c.Context(), params[0], params[1]); err != nil {
return err
}
}
return nil
},
}
1 change: 1 addition & 0 deletions cmd/celestia/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func init() {
false,
"Print JSON-RPC request along with the response",
)
rpcCmd.AddCommand(logCmd, logModuleCmd)
rpcCmd.AddCommand(blobCmd)
rootCmd.AddCommand(rpcCmd)
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/flags_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
)

var (
logLevelFlag = "log.level"
logLevelModuleFlag = "log.level.module"
LogLevelFlag = "log.level"
LogLevelModuleFlag = "log.level.module"
pprofFlag = "pprof"
tracingFlag = "tracing"
tracingEndpointFlag = "tracing.endpoint"
Expand All @@ -40,14 +40,14 @@ func MiscFlags() *flag.FlagSet {
flags := &flag.FlagSet{}

flags.String(
logLevelFlag,
LogLevelFlag,
"INFO",
`DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL
and their lower-case forms`,
)

flags.StringSlice(
logLevelModuleFlag,
LogLevelModuleFlag,
nil,
"<module>:<level>, e.g. pubsub:debug",
)
Expand Down Expand Up @@ -123,24 +123,24 @@ and their lower-case forms`,

// ParseMiscFlags parses miscellaneous flags from the given cmd and applies values to Env.
func ParseMiscFlags(ctx context.Context, cmd *cobra.Command) (context.Context, error) {
logLevel := cmd.Flag(logLevelFlag).Value.String()
logLevel := cmd.Flag(LogLevelFlag).Value.String()
if logLevel != "" {
level, err := logging.LevelFromString(logLevel)
if err != nil {
return ctx, fmt.Errorf("cmd: while parsing '%s': %w", logLevelFlag, err)
return ctx, fmt.Errorf("cmd: while parsing '%s': %w", LogLevelFlag, err)
}

logs.SetAllLoggers(level)
}

logModules, err := cmd.Flags().GetStringSlice(logLevelModuleFlag)
logModules, err := cmd.Flags().GetStringSlice(LogLevelModuleFlag)
if err != nil {
panic(err)
}
for _, ll := range logModules {
params := strings.Split(ll, ":")
if len(params) != 2 {
return ctx, fmt.Errorf("cmd: %s arg must be in form <module>:<level>, e.g. pubsub:debug", logLevelModuleFlag)
return ctx, fmt.Errorf("cmd: %s arg must be in form <module>:<level>, e.g. pubsub:debug", LogLevelModuleFlag)
}

err := logging.SetLogLevel(params[0], params[1])
Expand Down

0 comments on commit bbe970f

Please sign in to comment.