-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
172 lines (137 loc) · 3.83 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
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
package log
import (
"context"
"strings"
"github.com/getsentry/sentry-go"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var defaultLogger *zap.Logger
func Initialize(level string, isDebug bool, sentryOptions *sentry.ClientOptions) error {
log, err := New(level, isDebug)
if err != nil {
return err
}
defaultLogger = log
// Init sentry
if sentryOptions != nil {
err := sentry.Init(*sentryOptions)
if err != nil {
return err
}
}
return nil
}
func New(level string, isDebug bool) (*zap.Logger, error) {
var config zap.Config
if isDebug {
config = zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
} else {
config = zap.NewProductionConfig()
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
}
// override log level by configuration
l := zap.ErrorLevel
switch strings.ToUpper(level) {
case "TRACE", "DEBUG":
l = zap.DebugLevel
case "INFO":
l = zap.InfoLevel
case "WARN":
l = zap.WarnLevel
}
config.Level = zap.NewAtomicLevelAt(l)
return config.Build()
}
func SetSentryHubOnContext(ctx context.Context) context.Context {
hub := sentry.CurrentHub().Clone()
return sentry.SetHubOnContext(ctx, hub)
}
func Debug(msg string, fields ...zap.Field) {
DefaultLogger().WithOptions(zap.AddCallerSkip(1)).Debug(msg, fields...)
}
func Info(msg string, fields ...zap.Field) {
info(msg, sentry.CurrentHub(), fields...)
}
func InfoWithContext(ctx context.Context, msg string, fields ...zap.Field) {
info(msg, sentry.GetHubFromContext(ctx), fields...)
}
func info(msg string, hub *sentry.Hub, fields ...zap.Field) {
DefaultLogger().WithOptions(zap.AddCallerSkip(1)).Info(msg, fields...)
// Add a breadcrumb
addBreadcrumb(hub, &sentry.Breadcrumb{
Message: msg,
Level: sentry.LevelInfo,
Data: zapFieldsToMap(fields),
})
}
func Warn(msg string, fields ...zap.Field) {
warn(msg, sentry.CurrentHub(), fields...)
}
func WarnWithContext(ctx context.Context, msg string, fields ...zap.Field) {
warn(msg, sentry.GetHubFromContext(ctx), fields...)
}
func warn(msg string, hub *sentry.Hub, fields ...zap.Field) {
DefaultLogger().WithOptions(zap.AddCallerSkip(1)).Warn(msg, fields...)
// Add a breadcrumb
addBreadcrumb(hub, &sentry.Breadcrumb{
Message: msg,
Level: sentry.LevelWarning,
Data: zapFieldsToMap(fields),
})
}
func Error(msg string, fields ...zap.Field) {
err(msg, sentry.CurrentHub(), fields...)
}
func ErrorWithContext(ctx context.Context, msg string, fields ...zap.Field) {
err(msg, sentry.GetHubFromContext(ctx), fields...)
}
func err(msg string, hub *sentry.Hub, fields ...zap.Field) {
DefaultLogger().WithOptions(zap.AddCallerSkip(1)).Error(msg, fields...)
// Add a breadcrumb
addBreadcrumb(hub, &sentry.Breadcrumb{
Message: msg,
Level: sentry.LevelError,
Data: zapFieldsToMap(fields),
})
}
func addBreadcrumb(hub *sentry.Hub, breadcrumb *sentry.Breadcrumb) {
if nil != hub {
hub.AddBreadcrumb(breadcrumb, nil)
} else {
sentry.AddBreadcrumb(breadcrumb)
}
}
func Panic(msg string, fields ...zap.Field) {
DefaultLogger().WithOptions(zap.AddCallerSkip(1)).Panic(msg, fields...)
}
func Fatal(msg string, fields ...zap.Field) {
DefaultLogger().WithOptions(zap.AddCallerSkip(1)).Fatal(msg, fields...)
}
func Sugar() *zap.SugaredLogger {
return DefaultLogger().Sugar()
}
func Sync() error {
return DefaultLogger().Sync()
}
func DefaultLogger() *zap.Logger {
if defaultLogger == nil {
panic("use logger without initializing")
}
return defaultLogger
}
// Convert zap fields to a map that Sentry can understand
func zapFieldsToMap(fields []zap.Field) map[string]interface{} {
data := make(map[string]interface{})
for _, field := range fields {
var value interface{}
encoder := zapcore.NewMapObjectEncoder()
field.AddTo(encoder)
for _, v := range encoder.Fields {
value = v
}
data[field.Key] = value
}
return data
}