Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/nmt-wrapper-spec' into nmt-wrapp…
Browse files Browse the repository at this point in the history
…er-spec
  • Loading branch information
staheri14 committed Mar 8, 2023
2 parents 593deae + 802271c commit 6b342c6
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion cmd/celestia-appd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ import (
dbm "github.com/tendermint/tm-db"
)

const EnvPrefix = "CELESTIA"
const (
EnvPrefix = "CELESTIA"

// FlagLogToFile specifies whether to log to file or not.
FlagLogToFile = "log-to-file"
)

// NewRootCmd creates a new root command for celestia-appd. It is called once in the
// main function.
Expand Down Expand Up @@ -99,11 +104,18 @@ func NewRootCmd() *cobra.Command {
return err
}

// optionally log to file by replaceing the default logger with a file logger
err = replaceLogger(cmd)
if err != nil {
return err
}

return overrideServerConfig(cmd)
},
SilenceUsage: true,
}

rootCmd.PersistentFlags().String(FlagLogToFile, "", "Write logs directly to a file. If empty, logs are written to stderr")
initRootCmd(rootCmd, encodingConfig)

return rootCmd
Expand Down Expand Up @@ -287,3 +299,25 @@ func createAppAndExport(

return capp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList)
}

// replaceLogger optionally replaces the logger with a file logger if the flag
// is set to something other than the default.
func replaceLogger(cmd *cobra.Command) error {
logFilePath, err := cmd.Flags().GetString(FlagLogToFile)
if err != nil {
return err
}

if logFilePath == "" {
return nil
}

file, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return err
}

sctx := server.GetServerContextFromCmd(cmd)
sctx.Logger = log.NewTMLogger(log.NewSyncWriter(file))
return server.SetCmdServerContext(cmd, sctx)
}

0 comments on commit 6b342c6

Please sign in to comment.