OSDN Git Service

Split logrus config out (#65)
authorLiu-Cheng Xu <xuliuchengxlc@gmail.com>
Sat, 21 Oct 2017 07:16:42 +0000 (15:16 +0800)
committerGuanghua Guo <1536310027@qq.com>
Sat, 21 Oct 2017 07:16:42 +0000 (15:16 +0800)
- Keep main.go as simple as possiable

- Add `BYTOM_DEBUG` environment variable to set the log level to DEBUG

cmd/bytomd/log_hook.go [new file with mode: 0644]
cmd/bytomd/main.go

diff --git a/cmd/bytomd/log_hook.go b/cmd/bytomd/log_hook.go
new file mode 100644 (file)
index 0000000..9505815
--- /dev/null
@@ -0,0 +1,44 @@
+package main
+
+import (
+       log "github.com/sirupsen/logrus"
+       "os"
+       "path"
+       "runtime"
+       "strings"
+)
+
+type ContextHook struct{}
+
+func (hook ContextHook) Levels() []log.Level {
+       return log.AllLevels
+}
+
+func (hook ContextHook) Fire(entry *log.Entry) error {
+       pc := make([]uintptr, 3, 3)
+       cnt := runtime.Callers(6, pc)
+
+       for i := 0; i < cnt; i++ {
+               fu := runtime.FuncForPC(pc[i] - 1)
+               name := fu.Name()
+               if !strings.Contains(name, "github.com/Sirupsen/log") {
+                       file, line := fu.FileLine(pc[i] - 1)
+                       entry.Data["file"] = path.Base(file)
+                       entry.Data["func"] = path.Base(name)
+                       entry.Data["line"] = line
+                       break
+               }
+       }
+       return nil
+}
+
+func init() {
+       log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
+
+       // If environment variable BYTOM_DEBUG is not empty,
+       // 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 59c7497..e6d7dcb 100644 (file)
@@ -2,44 +2,11 @@ package main
 
 import (
        "github.com/bytom/cmd/bytomd/commands"
-       "github.com/sirupsen/logrus"
        "github.com/tendermint/tmlibs/cli"
        "os"
-       "path"
-       "runtime"
-       "strings"
 )
 
-type ContextHook struct{}
-
-func (hook ContextHook) Levels() []logrus.Level {
-       return logrus.AllLevels
-}
-
-func (hook ContextHook) Fire(entry *logrus.Entry) error {
-       pc := make([]uintptr, 3, 3)
-       cnt := runtime.Callers(6, pc)
-
-       for i := 0; i < cnt; i++ {
-               fu := runtime.FuncForPC(pc[i] - 1)
-               name := fu.Name()
-               if !strings.Contains(name, "github.com/Sirupsen/logrus") {
-                       file, line := fu.FileLine(pc[i] - 1)
-                       entry.Data["file"] = path.Base(file)
-                       entry.Data["func"] = path.Base(name)
-                       entry.Data["line"] = line
-                       break
-               }
-       }
-       return nil
-}
-
-func init() {
-       logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
-}
-
 func main() {
-       logrus.AddHook(ContextHook{})
        cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv("./.bytomd"))
        cmd.Execute()
 }