-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaller.go
127 lines (108 loc) · 2.88 KB
/
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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package logger
import (
"context"
"runtime"
"strconv"
"strings"
"go.uber.org/zap"
)
const loggerPackage = "github.com/things-go/logger"
type CallerCore struct {
level AtomicLevel
Skip int
SkipPackages []string
Caller func(depth int, skipPackages ...string) Field
}
func NewCallerCore() *CallerCore {
return &CallerCore{
level: NewAtomicLevelAt(ErrorLevel),
Skip: 0,
SkipPackages: nil,
Caller: DefaultCallerFile,
}
}
// AddSkip add the number of callers skipped by caller annotation.
func (c *CallerCore) AddSkip(callerSkip int) *CallerCore {
c.Skip += callerSkip
return c
}
// AddSkipPackage add the caller skip package.
func (c *CallerCore) AddSkipPackage(vs ...string) *CallerCore {
c.SkipPackages = append(c.SkipPackages, vs...)
return c
}
// SetLevel set the caller level.
func (c *CallerCore) SetLevel(lv Level) *CallerCore {
c.level.SetLevel(lv)
return c
}
// Level returns the minimum enabled log level.
func (c *CallerCore) Level() Level {
return c.level.Level()
}
// Enabled returns true if the given level is at or above this level.
func (c *CallerCore) Enabled(lvl Level) bool {
return c.level.Enabled(lvl)
}
// UseExternalLevel use external level, which controller by user.
func (c *CallerCore) UseExternalLevel(l AtomicLevel) *CallerCore {
c.level = l
return c
}
// UnderlyingLevel get underlying level.
func (c *CallerCore) UnderlyingLevel() AtomicLevel {
return c.level
}
// DefaultCallerFile caller file.
func DefaultCallerFile(depth int, skipPackages ...string) Field {
var file string
var line int
var ok bool
for i := depth; i < depth+10; i++ {
_, file, line, ok = runtime.Caller(i)
if ok && !skipPackage(file, skipPackages...) {
break
}
}
return zap.String("file", file+":"+strconv.Itoa(line))
}
// DefaultCaller caller.
func DefaultCaller(depth int, skipPackages ...string) Field {
var file string
var line int
var ok bool
for i := depth; i < depth+10; i++ {
_, file, line, ok = runtime.Caller(i)
if ok && !skipPackage(file, skipPackages...) {
break
}
}
idx := strings.LastIndexByte(file, '/')
return zap.String("caller", file[idx+1:]+":"+strconv.Itoa(line))
}
// File returns a Valuer that returns a pkg/file:line description of the caller.
func File(depth int, skipPackages ...string) Valuer {
return func(context.Context) Field {
return DefaultCallerFile(depth, skipPackages...)
}
}
// Caller returns a Valuer that returns a pkg/file:line description of the caller.
func Caller(depth int, skipPackages ...string) Valuer {
return func(context.Context) Field {
return DefaultCaller(depth, skipPackages...)
}
}
func skipPackage(file string, skipPackages ...string) bool {
if strings.HasSuffix(file, "_test.go") {
return false
}
if strings.Contains(file, loggerPackage) {
return true
}
for _, p := range skipPackages {
if strings.Contains(file, p) {
return true
}
}
return false
}