-
Notifications
You must be signed in to change notification settings - Fork 203
/
hooker_function.go
83 lines (76 loc) · 2.01 KB
/
hooker_function.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (C) 2017 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// the go-nebulas library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//
package logging
import (
"path/filepath"
"runtime"
"strings"
logrus "github.com/sirupsen/logrus"
)
type functionHooker struct {
innerLogger *logrus.Logger
file string
}
func (h *functionHooker) Fire(entry *logrus.Entry) error {
pc := make([]uintptr, 10)
runtime.Callers(6, pc)
for i := 0; i < 10; i++ {
if pc[i] == 0 {
break
}
f := runtime.FuncForPC(pc[i])
file, line := f.FileLine(pc[i])
if strings.Contains(file, "sirupsen") {
continue
}
fname := f.Name()
if strings.Contains(fname, "/") {
index := strings.LastIndex(fname, "/")
entry.Data["func"] = fname[index+1:]
// entry.Data["package"] = fname[0:index]
} else {
entry.Data["func"] = fname
}
entry.Data["line"] = line
entry.Data["file"] = filepath.Base(file)
break
}
return nil
}
func (h *functionHooker) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
}
}
// LoadFunctionHooker loads a function hooker to the logger
func LoadFunctionHooker(logger *logrus.Logger) {
_, file, _, ok := runtime.Caller(1)
if !ok {
file = "unknown"
}
inst := &functionHooker{
innerLogger: logger,
file: file,
}
logger.Hooks.Add(inst)
}