-
Notifications
You must be signed in to change notification settings - Fork 3
/
scope.go
294 lines (246 loc) · 8.16 KB
/
scope.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) Tetrate, Inc 2018 All Rights Reserved.
// Copyright 2018 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log // nolint: golint
import (
"fmt"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Scope let's you log data for an area of code, enabling the user full control over
// the level of logging output produced.
type Scope struct {
// immutable, set at creation
name string
nameToEmit string
description string
callerSkip int
// set by the Configure method and adjustable dynamically
outputLevel atomic.Value
stackTraceLevel atomic.Value
logCallers atomic.Value
}
var scopes = make(map[string]*Scope)
var lock = sync.Mutex{}
// set by the Configure method
var writeFn atomic.Value
var errorSink atomic.Value
// RegisterScope registers a new logging scope. If the same name is used multiple times
// for a single process, the same Scope struct is returned.
//
// Scope names cannot include colons, commas, or periods.
func RegisterScope(name string, description string, callerSkip int) *Scope {
if strings.ContainsAny(name, ":,.") {
return nil
}
lock.Lock()
defer lock.Unlock()
s, ok := scopes[name]
if !ok {
s = &Scope{
name: name,
description: description,
callerSkip: callerSkip,
}
s.SetOutputLevel(InfoLevel)
s.SetStackTraceLevel(NoneLevel)
s.SetLogCallers(false)
if name != DefaultScopeName {
s.nameToEmit = name
}
scopes[name] = s
}
return s
}
// FindScope returns a previously registered scope, or nil if the named scope wasn't previously registered
func FindScope(scope string) *Scope {
lock.Lock()
defer lock.Unlock()
s := scopes[scope]
return s
}
// Scopes returns a snapshot of the currently defined set of scopes
func Scopes() map[string]*Scope {
lock.Lock()
defer lock.Unlock()
s := make(map[string]*Scope, len(scopes))
for k, v := range scopes {
s[k] = v
}
return s
}
// Error outputs a message at error level.
func (s *Scope) Error(msg string, fields ...zapcore.Field) {
if s.GetOutputLevel() >= ErrorLevel {
s.emit(zapcore.ErrorLevel, s.GetStackTraceLevel() >= ErrorLevel, msg, fields)
}
}
// Errora uses fmt.Sprint to construct and log a message at error level.
func (s *Scope) Errora(args ...interface{}) {
if s.GetOutputLevel() >= ErrorLevel {
s.emit(zapcore.ErrorLevel, s.GetStackTraceLevel() >= ErrorLevel, fmt.Sprint(args...), nil)
}
}
// Errorf uses fmt.Sprintf to construct and log a message at error level.
func (s *Scope) Errorf(template string, args ...interface{}) {
if s.GetOutputLevel() >= ErrorLevel {
msg := template
if len(args) > 0 {
msg = fmt.Sprintf(template, args...)
}
s.emit(zapcore.ErrorLevel, s.GetStackTraceLevel() >= ErrorLevel, msg, nil)
}
}
// ErrorEnabled returns whether output of messages using this scope is currently enabled for error-level output.
func (s *Scope) ErrorEnabled() bool {
return s.GetOutputLevel() >= ErrorLevel
}
// Warn outputs a message at warn level.
func (s *Scope) Warn(msg string, fields ...zapcore.Field) {
if s.GetOutputLevel() >= WarnLevel {
s.emit(zapcore.WarnLevel, s.GetStackTraceLevel() >= ErrorLevel, msg, fields)
}
}
// Warna uses fmt.Sprint to construct and log a message at warn level.
func (s *Scope) Warna(args ...interface{}) {
if s.GetOutputLevel() >= WarnLevel {
s.emit(zapcore.WarnLevel, s.GetStackTraceLevel() >= ErrorLevel, fmt.Sprint(args...), nil)
}
}
// Warnf uses fmt.Sprintf to construct and log a message at warn level.
func (s *Scope) Warnf(template string, args ...interface{}) {
if s.GetOutputLevel() >= WarnLevel {
msg := template
if len(args) > 0 {
msg = fmt.Sprintf(template, args...)
}
s.emit(zapcore.WarnLevel, s.GetStackTraceLevel() >= ErrorLevel, msg, nil)
}
}
// WarnEnabled returns whether output of messages using this scope is currently enabled for warn-level output.
func (s *Scope) WarnEnabled() bool {
return s.GetOutputLevel() >= WarnLevel
}
// Info outputs a message at info level.
func (s *Scope) Info(msg string, fields ...zapcore.Field) {
if s.GetOutputLevel() >= InfoLevel {
s.emit(zapcore.InfoLevel, s.GetStackTraceLevel() >= ErrorLevel, msg, fields)
}
}
// Infoa uses fmt.Sprint to construct and log a message at info level.
func (s *Scope) Infoa(args ...interface{}) {
if s.GetOutputLevel() >= InfoLevel {
s.emit(zapcore.InfoLevel, s.GetStackTraceLevel() >= ErrorLevel, fmt.Sprint(args...), nil)
}
}
// Infof uses fmt.Sprintf to construct and log a message at info level.
func (s *Scope) Infof(template string, args ...interface{}) {
if s.GetOutputLevel() >= InfoLevel {
msg := template
if len(args) > 0 {
msg = fmt.Sprintf(template, args...)
}
s.emit(zapcore.InfoLevel, s.GetStackTraceLevel() >= ErrorLevel, msg, nil)
}
}
// InfoEnabled returns whether output of messages using this scope is currently enabled for info-level output.
func (s *Scope) InfoEnabled() bool {
return s.GetOutputLevel() >= InfoLevel
}
// Debug outputs a message at debug level.
func (s *Scope) Debug(msg string, fields ...zapcore.Field) {
if s.GetOutputLevel() >= DebugLevel {
s.emit(zapcore.DebugLevel, s.GetStackTraceLevel() >= ErrorLevel, msg, fields)
}
}
// Debuga uses fmt.Sprint to construct and log a message at debug level.
func (s *Scope) Debuga(args ...interface{}) {
if s.GetOutputLevel() >= DebugLevel {
s.emit(zapcore.DebugLevel, s.GetStackTraceLevel() >= ErrorLevel, fmt.Sprint(args...), nil)
}
}
// Debugf uses fmt.Sprintf to construct and log a message at debug level.
func (s *Scope) Debugf(template string, args ...interface{}) {
if s.GetOutputLevel() >= DebugLevel {
msg := template
if len(args) > 0 {
msg = fmt.Sprintf(template, args...)
}
s.emit(zapcore.DebugLevel, s.GetStackTraceLevel() >= ErrorLevel, msg, nil)
}
}
// DebugEnabled returns whether output of messages using this scope is currently enabled for debug-level output.
func (s *Scope) DebugEnabled() bool {
return s.GetOutputLevel() >= DebugLevel
}
// Name returns this scope's name.
func (s *Scope) Name() string {
return s.name
}
// Description returns this scope's description
func (s *Scope) Description() string {
return s.description
}
const callerSkipOffset = 2
func (s *Scope) emit(level zapcore.Level, dumpStack bool, msg string, fields []zapcore.Field) {
e := zapcore.Entry{
Message: msg,
Level: level,
Time: time.Now(),
LoggerName: s.nameToEmit,
}
if s.GetLogCallers() {
e.Caller = zapcore.NewEntryCaller(runtime.Caller(s.callerSkip + callerSkipOffset))
}
if dumpStack {
e.Stack = zap.Stack("").String
}
if w := writeFn.Load().(func(zapcore.Entry, []zapcore.Field) error); w != nil {
if err := w(e, fields); err != nil {
if es := errorSink.Load().(zapcore.WriteSyncer); es != nil {
_, _ = fmt.Fprintf(es, "%v log write error: %v\n", time.Now(), err)
_ = es.Sync()
}
}
}
}
// SetOutputLevel adjusts the output level associated with the scope.
func (s *Scope) SetOutputLevel(l Level) {
s.outputLevel.Store(l)
}
// GetOutputLevel returns the output level associated with the scope.
func (s *Scope) GetOutputLevel() Level {
return s.outputLevel.Load().(Level)
}
// SetStackTraceLevel adjusts the stack tracing level associated with the scope.
func (s *Scope) SetStackTraceLevel(l Level) {
s.stackTraceLevel.Store(l)
}
// GetStackTraceLevel returns the stack tracing level associated with the scope.
func (s *Scope) GetStackTraceLevel() Level {
return s.stackTraceLevel.Load().(Level)
}
// SetLogCallers adjusts the output level associated with the scope.
func (s *Scope) SetLogCallers(logCallers bool) {
s.logCallers.Store(logCallers)
}
// GetLogCallers returns the output level associated with the scope.
func (s *Scope) GetLogCallers() bool {
return s.logCallers.Load().(bool)
}