-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (47 loc) · 1.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"os"
"path"
"runtime"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/tendermint/tmlibs/cli"
"github.com/janotchain/janeta/cmd/janetad/commands"
"github.com/janotchain/janeta/config"
)
// 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{TimestampFormat: time.StampMilli, DisableColors: true})
// If environment variable janeta_DEBUG is not empty,
// then add the hook to logrus and set the log level to DEBUG
if os.Getenv("janeta_DEBUG") != "" {
log.AddHook(ContextHook{})
}
}
func main() {
cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv(config.DefaultDataDir()))
cmd.Execute()
}