From 8f5e3e4ae200dcb10aaf45d5ce02f3d8b1edf037 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Mon, 5 Feb 2018 09:33:45 +0800 Subject: [PATCH] Don't init logrus in a seperate file (#361) The logging funcationality is broken when crossing compile the executable file due to some unknown machnisms related to the main package. So, move `log_hook.go` to `main.go`. --- cmd/bytomd/log_hook.go | 44 -------------------------------------------- cmd/bytomd/main.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 44 deletions(-) delete mode 100644 cmd/bytomd/log_hook.go diff --git a/cmd/bytomd/log_hook.go b/cmd/bytomd/log_hook.go deleted file mode 100644 index 95058159..00000000 --- a/cmd/bytomd/log_hook.go +++ /dev/null @@ -1,44 +0,0 @@ -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) - } -} diff --git a/cmd/bytomd/main.go b/cmd/bytomd/main.go index cf51c1d9..3500898e 100644 --- a/cmd/bytomd/main.go +++ b/cmd/bytomd/main.go @@ -2,12 +2,54 @@ package main import ( "os" + "path" + "runtime" + "strings" + log "github.com/sirupsen/logrus" "github.com/tendermint/tmlibs/cli" "github.com/bytom/cmd/bytomd/commands" ) +// ContextHook is a hook for logrus. +type ContextHook struct{} + +// Levels returns the whole levels. +func (hook ContextHook) Levels() []log.Level { + return log.AllLevels +} + +// Fire helps logrus record the related file, function and line. +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) + } +} + func main() { cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv("./.bytomd")) cmd.Execute() -- 2.11.0