-
Notifications
You must be signed in to change notification settings - Fork 48
/
module.go
160 lines (142 loc) · 4.37 KB
/
module.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
package zap
import (
"context"
"flamingo.me/dingo"
"flamingo.me/flamingo/v3/framework/config"
"flamingo.me/flamingo/v3/framework/flamingo"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type (
// Module for logrus logging
Module struct {
area string
json bool
logLevel string
coloredOutput bool
developmentMode bool
samplingEnabled bool
samplingInitial float32
samplingThereafter float32
fieldMap map[string]string
}
shutdownEventSubscriber struct {
logger flamingo.Logger
}
)
var logLevels = map[string]zapcore.Level{
"Debug": zap.DebugLevel,
"Info": zap.InfoLevel,
"Warn": zap.WarnLevel,
"Error": zap.ErrorLevel,
"DPanic": zap.DPanicLevel,
"Panic": zap.PanicLevel,
"Fatal": zap.FatalLevel,
}
// Inject dependencies
func (m *Module) Inject(config *struct {
Area string `inject:"config:area"`
JSON bool `inject:"config:zap.json,optional"`
LogLevel string `inject:"config:zap.loglevel,optional"`
ColoredOutput bool `inject:"config:zap.colored,optional"`
DevelopmentMode bool `inject:"config:zap.devmode,optional"`
SamplingEnabled bool `inject:"config:zap.sampling.enabled,optional"`
SamplingInitial float32 `inject:"config:zap.sampling.initial,optional"`
SamplingThereafter float32 `inject:"config:zap.sampling.thereafter,optional"`
FieldMap config.Map `inject:"config:zap.fieldmap,optional"`
}) {
m.area = config.Area
m.json = config.JSON
m.logLevel = config.LogLevel
m.coloredOutput = config.ColoredOutput
m.developmentMode = config.DevelopmentMode
m.samplingEnabled = config.SamplingEnabled
m.samplingInitial = config.SamplingInitial
m.samplingThereafter = config.SamplingThereafter
if config.FieldMap != nil {
m.fieldMap = make(map[string]string, len(config.FieldMap))
for k, v := range config.FieldMap {
if v, ok := v.(string); ok {
m.fieldMap[k] = v
}
}
}
}
// Configure the logrus logger as flamingo.Logger (in JSON mode kibana compatible)
func (m *Module) Configure(injector *dingo.Injector) {
level, ok := logLevels[m.logLevel]
if !ok {
// if nothing is configured user ErrorLevel
level = zap.ErrorLevel
}
var samplingConfig *zap.SamplingConfig
if m.samplingEnabled {
samplingConfig = &zap.SamplingConfig{
Initial: int(m.samplingInitial),
Thereafter: int(m.samplingThereafter),
}
}
output := "console"
if m.json {
output = "json"
}
encoder := zapcore.CapitalLevelEncoder
if m.coloredOutput {
encoder = zapcore.CapitalColorLevelEncoder
}
cfg := zap.Config{
Level: zap.NewAtomicLevelAt(level),
Development: m.developmentMode,
DisableCaller: false,
DisableStacktrace: false,
Sampling: samplingConfig,
Encoding: output,
EncoderConfig: zapcore.EncoderConfig{
MessageKey: flamingo.LogKeyMessage,
LevelKey: flamingo.LogKeyLevel,
TimeKey: flamingo.LogKeyTimestamp,
NameKey: "logger",
CallerKey: flamingo.LogKeySource,
StacktraceKey: flamingo.LogKeyTrace,
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: encoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: zapcore.FullNameEncoder,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
InitialFields: nil,
}
logger, err := cfg.Build(zap.AddCallerSkip(1))
if err != nil {
panic(err)
}
zapLogger := &Logger{
Logger: logger,
fieldMap: m.fieldMap,
}
zapLogger = zapLogger.WithField(flamingo.LogKeyArea, m.area).(*Logger)
injector.Bind(new(flamingo.Logger)).ToInstance(zapLogger)
flamingo.BindEventSubscriber(injector).To(shutdownEventSubscriber{})
}
// Inject dependencies
func (subscriber *shutdownEventSubscriber) Inject(logger flamingo.Logger) {
subscriber.logger = logger
}
// Notify handles the incoming event if it is a AppShutdownEvent
func (subscriber *shutdownEventSubscriber) Notify(_ context.Context, event flamingo.Event) {
if _, ok := event.(*flamingo.ShutdownEvent); ok {
if logger, ok := subscriber.logger.(*Logger); ok {
logger.Debug("Zap Logger shutdown event")
logger.Sync()
}
}
}
// DefaultConfig for zap log level
func (m *Module) DefaultConfig() config.Map {
return config.Map{
"zap.loglevel": "Debug",
}
}