-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlogger.go
78 lines (63 loc) · 1.43 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
package zapion
import (
"sync"
"github.com/pion/logging"
"go.uber.org/zap"
)
type logger struct {
*zap.SugaredLogger
wrapped *zap.SugaredLogger
enableTrace bool
}
func (l *logger) Trace(msg string) {
if l.enableTrace {
l.wrapped.Debug(msg)
}
}
func (l *logger) Tracef(format string, args ...interface{}) {
if l.enableTrace {
l.wrapped.Debugf(format, args...)
}
}
func (l *logger) Debug(msg string) {
l.wrapped.Debug(msg)
}
func (l *logger) Info(msg string) {
l.wrapped.Info(msg)
}
func (l *logger) Warn(msg string) {
l.wrapped.Warn(msg)
}
func (l *logger) Error(msg string) {
l.wrapped.Error(msg)
}
// ZapFactory is a logger factory backended by zap logger.
type ZapFactory struct {
BaseLogger *zap.Logger
EnableTrace bool
mu sync.Mutex
loggers []*logger
}
// NewLogger creates new scoped logger.
func (f *ZapFactory) NewLogger(scope string) logging.LeveledLogger {
f.mu.Lock()
defer f.mu.Unlock()
named := f.BaseLogger.Named(scope)
l := &logger{
SugaredLogger: named.Sugar(),
wrapped: named.WithOptions(zap.AddCallerSkip(1)).Sugar(),
enableTrace: f.EnableTrace,
}
f.loggers = append(f.loggers, l)
return l
}
// SyncAll calls Sync() method of all child loggers.
// It is recommended to call this before exiting the program to
// ensure never loosing buffered log.
func (f *ZapFactory) SyncAll() {
f.mu.Lock()
defer f.mu.Unlock()
for _, l := range f.loggers {
_ = l.SugaredLogger.Sync()
}
}