Skip to content

Commit

Permalink
cmd: add init logger in printFlags (#1277)
Browse files Browse the repository at this point in the history
Inits logging as soon as log flags are parsed in printFlags function.

category: bug
ticket: #1272
  • Loading branch information
dB2510 committed Oct 13, 2022
1 parent 29d32bd commit bffcbb5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
3 changes: 0 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ func Run(ctx context.Context, conf Config) (err error) {
}()

_, _ = maxprocs.Set()
if err := log.InitLogger(conf.Log); err != nil {
return err
}

if err := featureset.Init(ctx, conf.Feature); err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmd/bootnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func newBootnodeCmd(runFunc func(context.Context, BootnodeConfig) error) *cobra.
Long: `Starts a discv5 bootnode that charon nodes can use to bootstrap their p2p cluster`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := initLogger(cmd.Flags()); err != nil {
return err
}

printFlags(cmd.Context(), cmd.Flags())

return runFunc(cmd.Context(), config)
Expand Down Expand Up @@ -91,10 +95,6 @@ func RunBootnode(ctx context.Context, config BootnodeConfig) error {

ctx = log.WithTopic(ctx, "bootnode")

if err := log.InitLogger(config.LogConfig); err != nil {
return err
}

version.LogInfo(ctx, "Charon bootnode starting")

key, err := p2p.LoadPrivKey(config.DataDir)
Expand Down
18 changes: 18 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,21 @@ func printFlags(ctx context.Context, flags *pflag.FlagSet) {

log.Info(ctx, "Parsed config", zStrs...)
}

// initLogger initialises logger based on provided log config flags.
func initLogger(flags *pflag.FlagSet) error {
logLevel := flags.Lookup("log-level")
logFmt := flags.Lookup("log-format")

if logLevel != nil && logFmt != nil {
err := log.InitLogger(log.Config{
Level: logLevel.Value.String(),
Format: logFmt.Value.String(),
})
if err != nil {
return err
}
}

return nil
}
4 changes: 4 additions & 0 deletions cmd/dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ distributed validator key shares and a final cluster lock configuration. Note th
this command at the same time.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := initLogger(cmd.Flags()); err != nil {
return err
}

printFlags(cmd.Context(), cmd.Flags())

return runFunc(cmd.Context(), config)
Expand Down
4 changes: 4 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func newRunCmd(runFunc func(context.Context, app.Config) error) *cobra.Command {
ctx, cancel := signal.NotifyContext(cmd.Context(), os.Interrupt)
defer cancel()

if err := initLogger(cmd.Flags()); err != nil {
return err
}

printFlags(ctx, cmd.Flags())

return runFunc(ctx, conf)
Expand Down
4 changes: 0 additions & 4 deletions dkg/dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ func Run(ctx context.Context, conf Config) (err error) {
}
}()

if err := log.InitLogger(conf.Log); err != nil {
return err
}

version.LogInfo(ctx, "Charon DKG starting")

def, err := loadDefinition(ctx, conf)
Expand Down

0 comments on commit bffcbb5

Please sign in to comment.