-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
logger.go
280 lines (223 loc) · 5.94 KB
/
logger.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// Package seclog holds seclog related files
package seclog
import (
"fmt"
"regexp"
"runtime"
"strings"
"sync"
"github.com/cihub/seelog"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const (
wildcard = "*"
depth = 4
)
// used to extract package.struct.func from the caller
var re = regexp.MustCompile(`[^\.]*\/([^\.]*)\.\(?\*?([^\.\)]*)\)?\.(.*)$`)
// TagStringer implements fmt.Stringer
type TagStringer struct {
tag string
}
// String implements fmt.Stringer
func (t *TagStringer) String() string {
return t.tag
}
// PatternLogger is a wrapper for the agent logger that add a level of filtering to trace log level
type PatternLogger struct {
sync.RWMutex
tags []string
patterns []string
nodes [][]string
}
func (l *PatternLogger) match(els []string) bool {
LOOP:
for _, pattern := range l.nodes {
for i, node := range pattern {
if node == wildcard {
continue
}
if i >= len(els) {
break
}
if node != els[i] {
continue LOOP
}
}
return true
}
return false
}
func (l *PatternLogger) trace(tag fmt.Stringer, format string, params ...interface{}) {
// check first tags
stag := tag.String()
if len(stag) != 0 {
l.RLock()
for _, t := range l.tags {
if t == stag {
l.RUnlock()
log.TraceStackDepth(depth, fmt.Sprintf(format, params...))
return
}
}
l.RUnlock()
}
pc, _, _, ok := runtime.Caller(3)
if !ok {
return
}
details := runtime.FuncForPC(pc)
if details == nil {
return
}
els := re.FindStringSubmatch(details.Name())
if len(els) != 4 {
return
}
l.RLock()
active := l.match(els[1:])
l.RUnlock()
if active {
log.TraceStackDepth(depth, fmt.Sprintf(format, params...))
}
}
// Trace is used to print a trace level log
func (l *PatternLogger) Trace(v interface{}) {
l.TraceTag(&TagStringer{}, v)
}
// TraceTag is used to print a trace level log for the given tag
func (l *PatternLogger) TraceTag(tag fmt.Stringer, v interface{}) {
l.TraceTagf(tag, "%v", v)
}
// TraceTagf is used to print a trace level log
func (l *PatternLogger) TraceTagf(tag fmt.Stringer, format string, params ...interface{}) {
if !l.IsTracing() {
return
}
l.trace(tag, format, params...)
}
// Tracef is used to print a trace level log
func (l *PatternLogger) Tracef(format string, params ...interface{}) {
if !l.IsTracing() {
return
}
l.trace(&TagStringer{}, format, params...)
}
// IsTracing is used to check if TraceF would actually log
func (l *PatternLogger) IsTracing() bool {
if logLevel, err := log.GetLogLevel(); err != nil || logLevel != seelog.TraceLvl {
return false
}
return true
}
// Debugf is used to print a trace level log
func (l *PatternLogger) Debugf(format string, params ...interface{}) {
log.DebugStackDepth(depth-1, fmt.Sprintf(format, params...))
}
// Errorf is used to print an error
func (l *PatternLogger) Errorf(format string, params ...interface{}) {
_ = log.ErrorStackDepth(depth-1, fmt.Sprintf(format, params...))
}
// Warnf is used to print a warn
func (l *PatternLogger) Warnf(format string, params ...interface{}) {
log.WarnStackDepth(depth-1, fmt.Sprintf(format, params...))
}
// Infof is used to print an error
func (l *PatternLogger) Infof(format string, params ...interface{}) {
log.InfoStackDepth(depth-1, fmt.Sprintf(format, params...))
}
// AddTags add new tags
func (l *PatternLogger) AddTags(tags ...string) []string {
l.Lock()
prev := l.tags
l.tags = append(l.tags, tags...)
l.Unlock()
return prev
}
// AddPatterns add new patterns
func (l *PatternLogger) AddPatterns(patterns ...string) []string {
l.Lock()
prev := l.patterns
for _, pattern := range patterns {
l.patterns = append(l.patterns, pattern)
l.nodes = append(l.nodes, strings.Split(pattern, "."))
}
l.Unlock()
return prev
}
// SetPatterns set patterns
func (l *PatternLogger) SetPatterns(patterns ...string) []string {
l.Lock()
prev := l.patterns
l.nodes = [][]string{}
for _, pattern := range patterns {
l.nodes = append(l.nodes, strings.Split(pattern, "."))
}
l.Unlock()
return prev
}
// SetTags set tags
func (l *PatternLogger) SetTags(tags ...string) []string {
l.Lock()
prev := l.tags
l.tags = tags
l.Unlock()
return prev
}
// DefaultLogger default logger of this package
var DefaultLogger *PatternLogger
// Debugf is used to print a trace level log
func Debugf(format string, params ...interface{}) {
DefaultLogger.Debugf(format, params...)
}
// Errorf is used to print an error
func Errorf(format string, params ...interface{}) {
DefaultLogger.Errorf(format, params...)
}
// Warnf is used to print a warn
func Warnf(format string, params ...interface{}) {
DefaultLogger.Warnf(format, params...)
}
// Infof is used to print an error
func Infof(format string, params ...interface{}) {
DefaultLogger.Infof(format, params...)
}
// Tracef is used to print an trace
func Tracef(format string, params ...interface{}) {
DefaultLogger.Tracef(format, params...)
}
// TraceTagf is used to print an trace
func TraceTagf(tag fmt.Stringer, format string, params ...interface{}) {
DefaultLogger.TraceTagf(tag, format, params...)
}
// Trace is used to print an trace
func Trace(v interface{}) {
DefaultLogger.Trace(v)
}
// TraceTag is used to print an trace
func TraceTag(tag fmt.Stringer, v interface{}) {
DefaultLogger.TraceTag(tag, v)
}
// AddTags add tags
func AddTags(tags ...string) []string {
return DefaultLogger.AddTags(tags...)
}
// AddPatterns add patterns
func AddPatterns(patterns ...string) []string {
return DefaultLogger.AddPatterns(patterns...)
}
// SetTags set tags
func SetTags(tags ...string) []string {
return DefaultLogger.SetTags(tags...)
}
// SetPatterns set patterns
func SetPatterns(patterns ...string) []string {
return DefaultLogger.SetPatterns(patterns...)
}
func init() {
DefaultLogger = &PatternLogger{}
}