-
Notifications
You must be signed in to change notification settings - Fork 1
/
cores.go
258 lines (219 loc) · 7.22 KB
/
cores.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
package zapper
import (
"fmt"
"github.com/TheZeroSlave/zapsentry"
"github.com/getsentry/sentry-go"
sentryotel "github.com/getsentry/sentry-go/otel"
color "github.com/mattn/go-colorable"
"github.com/natefinch/lumberjack"
"go.opentelemetry.io/otel"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"path/filepath"
"time"
)
//go:generate stringer -type=coreType
type (
coreType int
SentryEnvironment int
)
const (
CONSOLE coreType = iota
SENTRY
FILE
JSON
)
const (
DEVELOPMENT SentryEnvironment = iota // DEVELOPMENT application environment
PRODUCTION // PRODUCTION application environment
)
// Rotation config log file rotation in log path
type Rotation struct {
MaxAge int // MaxAge is the maximum number of days to retain old log files based on the timestamp encoded in their filename. Note that a day is defined as 24 hours and may not exactly correspond to calendar days due to daylight savings, leap seconds, etc. The default is not to remove old log files based on age.
FileSize int // FileSize is the maximum size in megabytes of the log file before it gets rotated. It defaults to 100 megabytes
Compress bool // Compress determines if the rotated log files should be compressed using gzip. The default is not to perform compression
}
// SentryConfig for sentry core set custom configs
type SentryConfig struct {
AttachStacktrace bool // AttachStacktrace attach stacktrace to event
Debug bool // Debug add debug data to event
EnableTracing bool
Environment string
Dist string
EnableBreadcrumbs bool
BreadcrumbLevel Level
MaxBreadcrumbs int
MaxSpans int
Tags map[string]string
MinLevel Level
FlushTimeout time.Duration
}
// Core zapper base abstract
type Core interface {
init(*Zap) error
}
type core struct {
coreType coreType
do func(*Zap) (zapcore.Core, error)
}
// ConsoleWriterCore create console writer for zapper to show log in console
func ConsoleWriterCore(colorable bool) Core {
return newCore(CONSOLE, func(z *Zap) (zapcore.Core, error) {
return zapcore.NewCore(encoder(z.development, colorable, z.timeFormat, func(cfg zapcore.EncoderConfig) zapcore.Encoder {
return zapcore.NewConsoleEncoder(cfg)
}), zapcore.AddSync(color.NewColorableStdout()), zap.LevelEnablerFunc(z.level)), nil
})
}
// SentryCore send log into sentry service
func SentryCore(dsn string, serverName string, environment SentryEnvironment, cfg *SentryConfig) Core {
if cfg == nil {
cfg = _defaultSentryConfig()
}
return newCore(SENTRY, func(zapper *Zap) (zapcore.Core, error) {
sentryCfg := sentry.ClientOptions{
Dsn: dsn,
AttachStacktrace: cfg.AttachStacktrace,
ServerName: serverName,
Debug: cfg.Debug,
EnableTracing: cfg.EnableTracing,
TracesSampleRate: 1.0,
Environment: environment.String(),
Dist: cfg.Dist,
MaxBreadcrumbs: cfg.MaxBreadcrumbs,
MaxSpans: cfg.MaxSpans,
}
ignoredErr := make([]string, 0)
switch cfg.MinLevel {
case Fatal, Panic, DPanic:
ignoredErr = append(ignoredErr, "error", "warning", "info", "debug")
case Error:
ignoredErr = append(ignoredErr, "warning", "info", "debug")
case Warn:
ignoredErr = append(ignoredErr, "info", "debug")
}
sentryCfg.IgnoreErrors = ignoredErr
s, err := sentry.NewClient(sentryCfg)
zapper.sentryClient = s
if cfg.EnableTracing {
tp := sdktrace.NewTracerProvider(
sdktrace.WithSpanProcessor(sentryotel.NewSentrySpanProcessor()),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(sentryotel.NewSentryPropagator())
}
if err != nil {
return nil, NewError("failed to create sentry client: %s", err.Error())
}
c, err := zapsentry.NewCore(zapsentry.Configuration{
Tags: cfg.Tags,
Level: cfg.MinLevel.zapLevel(),
EnableBreadcrumbs: cfg.EnableBreadcrumbs,
BreadcrumbLevel: cfg.BreadcrumbLevel.zapLevel(),
DisableStacktrace: false,
FlushTimeout: cfg.FlushTimeout,
}, zapsentry.NewSentryClientFromClient(s))
if err != nil {
return nil, NewError("failed to create core sentry: %s", err.Error())
}
return c, nil
})
}
// FileWriterCore write logs into file
func FileWriterCore(logPath string, rotation *Rotation) Core {
return newCore(FILE, func(z *Zap) (zapcore.Core, error) {
if _, err := filepath.Abs(logPath); err != nil {
return nil, NewError("logPath is invalid absolute path: %s", err.Error())
}
if rotation == nil {
rotation = _defaultRotation()
}
syncer, err := fileWriteSyncer(logPath, ".log", z.service.Name, rotation)
if err != nil {
return nil, NewError("failed to create log path: %s", err.Error())
}
return zapcore.NewCore(encoder(z.development, false, z.timeFormat, func(cfg zapcore.EncoderConfig) zapcore.Encoder {
return zapcore.NewConsoleEncoder(cfg)
}), zapcore.Lock(syncer), zap.LevelEnablerFunc(z.level)), nil
})
}
// JsonWriterCore write logs with json format, fileExtension is for set output file extension json,log and etc
func JsonWriterCore(logPath, fileExtension string, rotation *Rotation) Core {
return newCore(JSON, func(z *Zap) (zapcore.Core, error) {
if _, err := filepath.Abs(logPath); err != nil {
return nil, NewError("logPath is invalid absolute path: %s", err.Error())
}
if rotation == nil {
rotation = _defaultRotation()
}
syncer, err := fileWriteSyncer(logPath, fileExtension, z.service.Name, rotation)
if err != nil {
return nil, NewError("failed to create log path: %s", err.Error())
}
return zapcore.NewCore(encoder(z.development, false, z.timeFormat, func(cfg zapcore.EncoderConfig) zapcore.Encoder {
return zapcore.NewJSONEncoder(cfg)
}), zapcore.Lock(syncer), zap.LevelEnablerFunc(z.level)), nil
})
}
func (e SentryEnvironment) String() string {
switch e {
case DEVELOPMENT:
return "Development"
case PRODUCTION:
return "Production"
default:
return "Development"
}
}
func (z *core) init(zapper *Zap) error {
c, err := z.do(zapper)
if err != nil {
return err
}
if z.coreType == SENTRY {
zapper.sentryCore = c
return nil
}
zapper.cores = append(zapper.cores, c)
return nil
}
func newCore(coreType coreType, f func(*Zap) (zapcore.Core, error)) *core {
return &core{
coreType: coreType,
do: f,
}
}
func fileWriteSyncer(logPath, extFile, serviceName string, rotation *Rotation) (zapcore.WriteSyncer, error) {
if len(serviceName) != 0 {
serviceName = fmt.Sprintf("%s_", serviceName)
}
path := filepath.Join(logPath, fmt.Sprintf("%s%s%s", serviceName, time.Now().Format("2006-01-02_15-04-05"), extFile))
if len(filepath.Ext(path)) == 0 {
return nil, NewError("file extension is invalid")
}
r := new(lumberjack.Logger)
r.Filename = path
r.MaxAge = rotation.MaxAge
r.MaxSize = rotation.FileSize
r.Compress = rotation.Compress
return zapcore.AddSync(r), nil
}
func _defaultRotation() *Rotation {
return &Rotation{
MaxAge: 1,
FileSize: 10,
Compress: false,
}
}
func _defaultSentryConfig() *SentryConfig {
return &SentryConfig{
AttachStacktrace: true,
Tags: map[string]string{
"component": "system",
},
MinLevel: Error,
EnableBreadcrumbs: true,
BreadcrumbLevel: Info,
FlushTimeout: 2 * time.Second,
}
}