forked from hyperledger/fabric-sdk-go
/
modlog.go
468 lines (412 loc) · 12.4 KB
/
modlog.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package modlog
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"io"
"sync/atomic"
"github.com/hyperledger/fabric-sdk-go/pkg/core/logging/api"
"github.com/hyperledger/fabric-sdk-go/pkg/core/logging/metadata"
)
var rwmutex = &sync.RWMutex{}
var moduleLevels = &metadata.ModuleLevels{}
var callerInfos = &metadata.CallerInfo{}
var useCustomLogger int32
// default logger factory singleton
var loggerProviderInstance api.LoggerProvider
var loggerProviderOnce sync.Once
// Provider is the default logger implementation
type Provider struct {
}
//GetLogger returns SDK logger implementation
func (p *Provider) GetLogger(module string) api.Logger {
newDefLogger := log.New(os.Stdout, fmt.Sprintf(logPrefixFormatter, module), log.Ldate|log.Ltime|log.LUTC)
return &Log{deflogger: newDefLogger, module: module}
}
//LoggerProvider returns logging provider for SDK logger
func LoggerProvider() api.LoggerProvider {
return &Provider{}
}
//InitLogger sets custom logger which will be used over deflogger.
//It is required to call this function before making any loggings.
func InitLogger(l api.LoggerProvider) {
loggerProviderOnce.Do(func() {
loggerProviderInstance = l
atomic.StoreInt32(&useCustomLogger, 1)
})
}
//Log is a standard SDK logger implementation
type Log struct {
deflogger *log.Logger
customLogger api.Logger
module string
custom bool
once sync.Once
}
//LoggerOpts for all logger customization options
type loggerOpts struct {
levelEnabled bool
callerInfoEnabled bool
}
const (
logLevelFormatter = "UTC %s-> %4.4s "
logPrefixFormatter = " [%s] "
callerInfoFormatter = "- %s "
)
//SetLevel - setting log level for given module
func SetLevel(module string, level api.Level) {
rwmutex.Lock()
defer rwmutex.Unlock()
moduleLevels.SetLevel(module, level)
}
//GetLevel - getting log level for given module
func GetLevel(module string) api.Level {
rwmutex.RLock()
defer rwmutex.RUnlock()
return moduleLevels.GetLevel(module)
}
//IsEnabledFor - Check if given log level is enabled for given module
func IsEnabledFor(module string, level api.Level) bool {
rwmutex.RLock()
defer rwmutex.RUnlock()
return moduleLevels.IsEnabledFor(module, level)
}
//ShowCallerInfo - Show caller info in log lines for given log level
func ShowCallerInfo(module string, level api.Level) {
rwmutex.Lock()
defer rwmutex.Unlock()
callerInfos.ShowCallerInfo(module, level)
}
//HideCallerInfo - Do not show caller info in log lines for given log level
func HideCallerInfo(module string, level api.Level) {
rwmutex.Lock()
defer rwmutex.Unlock()
callerInfos.HideCallerInfo(module, level)
}
//getLoggerOpts - returns LoggerOpts which can be used for customization
func getLoggerOpts(module string, level api.Level) *loggerOpts {
rwmutex.RLock()
defer rwmutex.RUnlock()
return &loggerOpts{
levelEnabled: moduleLevels.IsEnabledFor(module, level),
callerInfoEnabled: callerInfos.IsCallerInfoEnabled(module, level),
}
}
// Fatal is CRITICAL log followed by a call to os.Exit(1).
func (l *Log) Fatal(args ...interface{}) {
opts := getLoggerOpts(l.module, api.CRITICAL)
if l.loadCustomLogger() {
l.customLogger.Fatal(args...)
return
}
l.log(opts, api.CRITICAL, args...)
l.deflogger.Fatal(args...)
}
// Fatalf is CRITICAL log formatted followed by a call to os.Exit(1).
func (l *Log) Fatalf(format string, args ...interface{}) {
opts := getLoggerOpts(l.module, api.CRITICAL)
if l.loadCustomLogger() {
l.customLogger.Fatalf(format, args...)
return
}
l.logf(opts, api.CRITICAL, format, args...)
l.deflogger.Fatalf(format, args...)
}
// Fatalln is CRITICAL log ln followed by a call to os.Exit(1).
func (l *Log) Fatalln(args ...interface{}) {
opts := getLoggerOpts(l.module, api.CRITICAL)
if l.loadCustomLogger() {
l.customLogger.Fatalln(args...)
return
}
l.logln(opts, api.CRITICAL, args...)
l.deflogger.Fatalln(args...)
}
// Panic is CRITICAL log followed by a call to panic()
func (l *Log) Panic(args ...interface{}) {
opts := getLoggerOpts(l.module, api.CRITICAL)
if l.loadCustomLogger() {
l.customLogger.Panic(args...)
return
}
l.log(opts, api.CRITICAL, args...)
l.deflogger.Panic(args...)
}
// Panicf is CRITICAL log formatted followed by a call to panic()
func (l *Log) Panicf(format string, args ...interface{}) {
opts := getLoggerOpts(l.module, api.CRITICAL)
if l.loadCustomLogger() {
l.customLogger.Panicf(format, args...)
return
}
l.logf(opts, api.CRITICAL, format, args...)
l.deflogger.Panicf(format, args...)
}
// Panicln is CRITICAL log ln followed by a call to panic()
func (l *Log) Panicln(args ...interface{}) {
opts := getLoggerOpts(l.module, api.CRITICAL)
if l.loadCustomLogger() {
l.customLogger.Panicln(args...)
return
}
l.logln(opts, api.CRITICAL, args...)
l.deflogger.Panicln(args...)
}
// Print calls go log.Output.
// Arguments are handled in the manner of fmt.Print.
func (l *Log) Print(args ...interface{}) {
if l.loadCustomLogger() {
l.customLogger.Print(args...)
return
}
l.deflogger.Print(args...)
}
// Printf calls go log.Output.
// Arguments are handled in the manner of fmt.Printf.
func (l *Log) Printf(format string, args ...interface{}) {
if l.loadCustomLogger() {
l.customLogger.Printf(format, args...)
return
}
l.deflogger.Printf(format, args...)
}
// Println calls go log.Output.
// Arguments are handled in the manner of fmt.Println.
func (l *Log) Println(args ...interface{}) {
if l.loadCustomLogger() {
l.customLogger.Println(args...)
return
}
l.deflogger.Println(args...)
}
// Debug calls go log.Output.
// Arguments are handled in the manner of fmt.Print.
func (l *Log) Debug(args ...interface{}) {
opts := getLoggerOpts(l.module, api.DEBUG)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Debug(args...)
return
}
l.log(opts, api.DEBUG, args...)
}
// Debugf calls go log.Output.
// Arguments are handled in the manner of fmt.Printf.
func (l *Log) Debugf(format string, args ...interface{}) {
opts := getLoggerOpts(l.module, api.DEBUG)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Debugf(format, args...)
return
}
l.logf(opts, api.DEBUG, format, args...)
}
// Debugln calls go log.Output.
// Arguments are handled in the manner of fmt.Println.
func (l *Log) Debugln(args ...interface{}) {
opts := getLoggerOpts(l.module, api.DEBUG)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Debugln(args...)
return
}
l.logln(opts, api.DEBUG, args...)
}
// Info calls go log.Output.
// Arguments are handled in the manner of fmt.Print.
func (l *Log) Info(args ...interface{}) {
opts := getLoggerOpts(l.module, api.INFO)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Info(args...)
return
}
l.log(opts, api.INFO, args...)
}
// Infof calls go log.Output.
// Arguments are handled in the manner of fmt.Printf.
func (l *Log) Infof(format string, args ...interface{}) {
opts := getLoggerOpts(l.module, api.INFO)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Infof(format, args...)
return
}
l.logf(opts, api.INFO, format, args...)
}
// Infoln calls go log.Output.
// Arguments are handled in the manner of fmt.Println.
func (l *Log) Infoln(args ...interface{}) {
opts := getLoggerOpts(l.module, api.INFO)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Infoln(args...)
return
}
l.logln(opts, api.INFO, args...)
}
// Warn calls go log.Output.
// Arguments are handled in the manner of fmt.Print.
func (l *Log) Warn(args ...interface{}) {
opts := getLoggerOpts(l.module, api.WARNING)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Warn(args...)
return
}
l.log(opts, api.WARNING, args...)
}
// Warnf calls go log.Output.
// Arguments are handled in the manner of fmt.Printf.
func (l *Log) Warnf(format string, args ...interface{}) {
opts := getLoggerOpts(l.module, api.WARNING)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Warnf(format, args...)
return
}
l.logf(opts, api.WARNING, format, args...)
}
// Warnln calls go log.Output.
// Arguments are handled in the manner of fmt.Println.
func (l *Log) Warnln(args ...interface{}) {
opts := getLoggerOpts(l.module, api.WARNING)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Warnln(args...)
return
}
l.logln(opts, api.WARNING, args...)
}
// Error calls go log.Output.
// Arguments are handled in the manner of fmt.Print.
func (l *Log) Error(args ...interface{}) {
opts := getLoggerOpts(l.module, api.ERROR)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Error(args...)
return
}
l.log(opts, api.ERROR, args...)
}
// Errorf calls go log.Output.
// Arguments are handled in the manner of fmt.Printf.
func (l *Log) Errorf(format string, args ...interface{}) {
opts := getLoggerOpts(l.module, api.ERROR)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Errorf(format, args...)
return
}
l.logf(opts, api.ERROR, format, args...)
}
// Errorln calls go log.Output.
// Arguments are handled in the manner of fmt.Println.
func (l *Log) Errorln(args ...interface{}) {
opts := getLoggerOpts(l.module, api.ERROR)
if !opts.levelEnabled {
return
}
if l.loadCustomLogger() {
l.customLogger.Errorln(args...)
return
}
l.logln(opts, api.ERROR, args...)
}
//ChangeOutput for changing output destination for the logger.
func (l *Log) ChangeOutput(output io.Writer) {
l.deflogger.SetOutput(output)
}
func (l *Log) logf(opts *loggerOpts, level api.Level, format string, args ...interface{}) {
//Format prefix to show function name and log level and to indicate that timezone used is UTC
customPrefix := fmt.Sprintf(logLevelFormatter, l.getCallerInfo(opts), metadata.ParseString(level))
err := l.deflogger.Output(2, customPrefix+fmt.Sprintf(format, args...))
if err != nil {
fmt.Printf("error from deflogger.Output %v\n", err)
}
}
func (l *Log) log(opts *loggerOpts, level api.Level, args ...interface{}) {
//Format prefix to show function name and log level and to indicate that timezone used is UTC
customPrefix := fmt.Sprintf(logLevelFormatter, l.getCallerInfo(opts), metadata.ParseString(level))
err := l.deflogger.Output(2, customPrefix+fmt.Sprint(args...))
if err != nil {
fmt.Printf("error from deflogger.Output %v\n", err)
}
}
func (l *Log) logln(opts *loggerOpts, level api.Level, args ...interface{}) {
//Format prefix to show function name and log level and to indicate that timezone used is UTC
customPrefix := fmt.Sprintf(logLevelFormatter, l.getCallerInfo(opts), metadata.ParseString(level))
err := l.deflogger.Output(2, customPrefix+fmt.Sprintln(args...))
if err != nil {
fmt.Printf("error from deflogger.Output %v\n", err)
}
}
func (l *Log) loadCustomLogger() bool {
l.once.Do(func() {
if atomic.LoadInt32(&useCustomLogger) > 0 {
l.customLogger = loggerProviderInstance.GetLogger(l.module)
l.custom = true
}
})
return l.custom
}
func (l *Log) getCallerInfo(opts *loggerOpts) string {
if !opts.callerInfoEnabled {
return ""
}
const MAXCALLERS = 6 // search MAXCALLERS frames for the real caller
const SKIPCALLERS = 4 // skip SKIPCALLERS frames when determining the real caller
const DEFAULTLOGPREFIX = "api.(Logger)" // LOGPREFIX indicates the upcoming frame contains the real caller and skip the frame
const LOGPREFIX = "logging.(*Logger)" // LOGPREFIX indicates the upcoming frame contains the real caller and skip the frame
const LOGBRIDGEPREFIX = "logbridge." // LOGBRIDGEPREFIX indicates to skip the frame due to being a logbridge
const NOTFOUND = "n/a"
fpcs := make([]uintptr, MAXCALLERS)
n := runtime.Callers(SKIPCALLERS, fpcs)
if n == 0 {
return fmt.Sprintf(callerInfoFormatter, NOTFOUND)
}
frames := runtime.CallersFrames(fpcs[:n])
funcIsNext := false
for f, more := frames.Next(); more; f, more = frames.Next() {
_, funName := filepath.Split(f.Function)
if f.Func == nil || f.Function == "" {
funName = NOTFOUND // not a function or unknown
}
if strings.HasPrefix(funName, LOGPREFIX) || strings.HasPrefix(funName, LOGBRIDGEPREFIX) || strings.HasPrefix(funName, DEFAULTLOGPREFIX) {
funcIsNext = true
} else if funcIsNext {
return fmt.Sprintf(callerInfoFormatter, funName)
}
}
return fmt.Sprintf(callerInfoFormatter, NOTFOUND)
}