github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/cmd/bytomd/main.go (about) 1 package main 2 3 import ( 4 "os" 5 "path" 6 "runtime" 7 "strings" 8 "time" 9 10 log "github.com/sirupsen/logrus" 11 "github.com/tendermint/tmlibs/cli" 12 13 "github.com/bytom/bytom/cmd/bytomd/commands" 14 "github.com/bytom/bytom/config" 15 ) 16 17 // ContextHook is a hook for logrus. 18 type ContextHook struct{} 19 20 // Levels returns the whole levels. 21 func (hook ContextHook) Levels() []log.Level { 22 return log.AllLevels 23 } 24 25 // Fire helps logrus record the related file, function and line. 26 func (hook ContextHook) Fire(entry *log.Entry) error { 27 pc := make([]uintptr, 3, 3) 28 cnt := runtime.Callers(6, pc) 29 30 for i := 0; i < cnt; i++ { 31 fu := runtime.FuncForPC(pc[i] - 1) 32 name := fu.Name() 33 if !strings.Contains(name, "github.com/Sirupsen/log") { 34 file, line := fu.FileLine(pc[i] - 1) 35 entry.Data["file"] = path.Base(file) 36 entry.Data["func"] = path.Base(name) 37 entry.Data["line"] = line 38 break 39 } 40 } 41 return nil 42 } 43 44 func init() { 45 log.SetFormatter(&log.TextFormatter{TimestampFormat: time.StampMilli, DisableColors: true}) 46 47 // If environment variable BYTOM_DEBUG is not empty, 48 // then add the hook to logrus and set the log level to DEBUG 49 if os.Getenv("BYTOM_DEBUG") != "" { 50 log.AddHook(ContextHook{}) 51 } 52 } 53 54 func main() { 55 cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv(config.DefaultDataDir())) 56 cmd.Execute() 57 }