OSDN Git Service

Add bytomd command line param about setting log level (#1115)
authorsuccessli <successli@outlook.com>
Tue, 3 Jul 2018 06:47:17 +0000 (14:47 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 3 Jul 2018 06:47:17 +0000 (14:47 +0800)
* add bytomd command line param about log level

* default set to info and rm pre-setting for log level

cmd/bytomd/commands/run_node.go
cmd/bytomd/main.go
config/config.go

index ac6caeb..b103fbf 100644 (file)
@@ -7,6 +7,7 @@ import (
        "github.com/spf13/cobra"
 
        "github.com/bytom/node"
+       "strings"
 )
 
 var runNodeCmd = &cobra.Command{
@@ -27,6 +28,9 @@ func init() {
        runNodeCmd.Flags().Bool("web.closed", config.Web.Closed, "Lanch web browser or not")
        runNodeCmd.Flags().String("chain_id", config.ChainID, "Select network type")
 
+       // log level
+       runNodeCmd.Flags().String("log_level", config.LogLevel, "Select log level(debug, info, warn, error or fatal")
+
        // p2p flags
        runNodeCmd.Flags().String("p2p.laddr", config.P2P.ListenAddress, "Node listen address. (0.0.0.0:0 means any interface, any port)")
        runNodeCmd.Flags().String("p2p.seeds", config.P2P.Seeds, "Comma delimited host:port seed nodes")
@@ -42,7 +46,27 @@ func init() {
        RootCmd.AddCommand(runNodeCmd)
 }
 
+func getLogLevel(level string) log.Level {
+       switch strings.ToLower(level) {
+       case "debug":
+               return log.DebugLevel
+       case "info":
+               return log.InfoLevel
+       case "warn":
+               return log.WarnLevel
+       case "error":
+               return log.ErrorLevel
+       case "fatal":
+               return log.FatalLevel
+       default:
+               return log.InfoLevel
+       }
+}
+
 func runNode(cmd *cobra.Command, args []string) error {
+       // Set log level by config.LogLevel
+       log.SetLevel(getLogLevel(config.LogLevel))
+
        // Create & start node
        n := node.NewNode(config)
        if _, err := n.Start(); err != nil {
index 6792c87..999ad2c 100644 (file)
@@ -47,7 +47,6 @@ func init() {
        // then add the hook to logrus and set the log level to DEBUG
        if os.Getenv("BYTOM_DEBUG") != "" {
                log.AddHook(ContextHook{})
-               log.SetLevel(log.DebugLevel)
        }
 }
 
index 62b6e53..e8a1ea7 100644 (file)
@@ -46,6 +46,9 @@ type BaseConfig struct {
        //The ID of the network to json
        ChainID string `mapstructure:"chain_id"`
 
+       //log level to set
+       LogLevel string `mapstructure:"log_level"`
+
        // A JSON file containing the private key to use as a validator in the consensus protocol
        PrivateKey string `mapstructure:"private_key"`