-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
197 lines (173 loc) · 4.44 KB
/
logger.go
File metadata and controls
197 lines (173 loc) · 4.44 KB
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
package logging
import (
"log"
"os"
"sync"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type WriteSyncer = zapcore.WriteSyncer
var defaultOutput WriteSyncer = os.Stderr
type Logger interface {
Output() WriteSyncer
SetOutput(WriteSyncer)
Debug(args ...interface{})
Info(args ...interface{})
Warn(args ...interface{})
Error(args ...interface{})
DPanic(args ...interface{})
Panic(args ...interface{})
Fatal(args ...interface{})
Debugf(template string, args ...interface{})
Infof(template string, args ...interface{})
Warnf(template string, args ...interface{})
Errorf(template string, args ...interface{})
DPanicf(template string, args ...interface{})
Panicf(template string, args ...interface{})
Fatalf(template string, args ...interface{})
Debugw(msg string, keysAndValues ...interface{})
Infow(msg string, keysAndValues ...interface{})
Warnw(msg string, keysAndValues ...interface{})
Errorw(msg string, keysAndValues ...interface{})
DPanicw(msg string, keysAndValues ...interface{})
Panicw(msg string, keysAndValues ...interface{})
Fatalw(msg string, keysAndValues ...interface{})
StdLogger() *log.Logger
Format() Format
SetFormat(Format)
Level() Level
SetLevel(Level)
LogRUsLogger() *logrus.Logger
ZapLogger() *zap.Logger
ZapSugaredLogger() *zap.SugaredLogger
With(args ...interface{}) Logger
}
func NewLogger(options ...Option) Logger {
l := &logger{
level: DefaultLevel,
format: DefaultFormat,
preset: DefaultPreset,
output: defaultOutput,
zapMutex: &sync.RWMutex{},
logrusMutex: &sync.RWMutex{},
stdMutex: &sync.RWMutex{},
}
// default zap pre-optiosn
if l.zap == nil {
cfg := zap.NewDevelopmentEncoderConfig()
encoder := zapcore.NewConsoleEncoder(cfg)
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(zapcore.ErrorLevel)
l.zapAtomicLevel = &atomicLevel
l.zap = zap.New(zapcore.NewCore(encoder, zapcore.Lock(os.Stderr), atomicLevel))
l.format = FormatText
}
l.SugaredLogger = l.zap.Sugar()
l.std = zap.NewStdLog(l.zap)
l.logrus = logrus.New()
l.logrus.SetFormatter(l.Format().LogRUsFormatter())
// to set logrus and atomic levels /formats if necessary
l.SetFormat(l.format)
l.SetLevel(l.level)
for _, o := range options {
o(l)
}
return l
}
type logger struct {
*zap.SugaredLogger
zap *zap.Logger
zapAtomicLevel *zap.AtomicLevel
std *log.Logger
logrus *logrus.Logger
format Format
level Level
output WriteSyncer
preset Preset
zapMutex *sync.RWMutex
logrusMutex *sync.RWMutex
stdMutex *sync.RWMutex
}
func (l *logger) resetZap() {
l.zapMutex.Lock()
defer l.zapMutex.Unlock()
cfg := l.Preset().ZapEncoderConfig()
encoder := l.Format().ZapEncoder(cfg)
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(l.Level().Zap())
l.zap = zap.New(zapcore.NewCore(encoder, zapcore.Lock(l.Output()), atomicLevel))
l.SugaredLogger = l.zap.Sugar()
}
func (l *logger) Output() WriteSyncer {
return l.output
}
func (l *logger) SetOutput(w WriteSyncer) {
if w == nil {
return
}
l.output = w
l.LogRUsLogger().SetOutput(w)
l.resetZap()
}
func (l *logger) Format() Format {
return l.format
}
func (l *logger) SetFormat(format Format) {
if !format.IsAFormat() {
return
}
l.format = format
l.LogRUsLogger().SetFormatter(format.LogRUsFormatter())
l.resetZap()
}
func (l *logger) Preset() Preset {
return l.preset
}
func (l *logger) SetPreset(p Preset) {
l.preset = p
}
func (l *logger) Level() Level {
return l.level
}
func (l *logger) SetLevel(level Level) {
if !level.IsALevel() {
return
}
l.level = level
if l.zapAtomicLevel != nil {
atomicLevel := *l.zapAtomicLevel
atomicLevel.SetLevel(level.Zap())
} else {
l.resetZap()
}
l.LogRUsLogger().SetLevel(level.LogRUs())
}
func (l *logger) With(args ...interface{}) Logger {
sl := l.ZapSugaredLogger().With(args...)
return NewLogger(
WithLevel(l.Level()),
WithPreset(l.Preset()),
WithFormat(l.Format()),
WithOutput(l.Output()),
WithZap(sl.Desugar()),
WithZapSugared(sl))
}
func (l *logger) ZapSugaredLogger() *zap.SugaredLogger {
l.zapMutex.RLock()
defer l.zapMutex.RUnlock()
return l.SugaredLogger
}
func (l *logger) ZapLogger() *zap.Logger {
l.zapMutex.RLock()
defer l.zapMutex.RUnlock()
return l.zap
}
func (l *logger) StdLogger() *log.Logger {
return l.std
}
func (l *logger) LogRUsLogger() *logrus.Logger {
l.logrusMutex.RLock()
defer l.logrusMutex.RUnlock()
return l.logrus
}