forked from seeleteam/go-seele
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caller.go
46 lines (37 loc) · 941 Bytes
/
caller.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
/**
* @file
* @copyright defined in go-seele/LICENSE
*/
package log
import (
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/sirupsen/logrus"
)
const (
depth = 9 // Once log invocation stack has changed, depth needs to change as well.
)
// CallerHook a caller hook of logrus
type CallerHook struct {
module string
}
// Fire adds a caller field in logger instance
func (hook *CallerHook) Fire(entry *logrus.Entry) error {
entry.Data["caller"] = hook.caller()
entry.Data["module"] = hook.module
return nil
}
// Levels returns supported levels
func (hook *CallerHook) Levels() []logrus.Level {
return logrus.AllLevels
}
// caller returns the invoker which is being executed
func (hook *CallerHook) caller() string {
if _, file, line, ok := runtime.Caller(depth); ok {
return strings.Join([]string{filepath.Base(file), strconv.Itoa(line)}, ":")
}
// not sure what the convention should be here
return ""
}