@@ -30,8 +30,31 @@ var now = time.Now
30
30
type Writer struct {
31
31
hub * sentry.Hub
32
32
33
- levels map [zerolog.Level ]struct {}
34
- flushTimeout time.Duration
33
+ levels map [zerolog.Level ]struct {}
34
+ flushTimeout time.Duration
35
+ withBreadcrumbs bool
36
+ }
37
+
38
+ // addBreadcrumb adds event as a breadcrumb
39
+ func (w * Writer ) addBreadcrumb (event * sentry.Event ) {
40
+ if ! w .withBreadcrumbs {
41
+ return
42
+ }
43
+
44
+ // category is totally optional, but it's nice to have
45
+ var category string
46
+ if _ , ok := event .Extra ["category" ]; ok {
47
+ if v , ok := event .Extra ["category" ].(string ); ok {
48
+ category = v
49
+ }
50
+ }
51
+
52
+ w .hub .AddBreadcrumb (& sentry.Breadcrumb {
53
+ Category : category ,
54
+ Message : event .Message ,
55
+ Level : event .Level ,
56
+ Data : event .Extra ,
57
+ }, nil )
35
58
}
36
59
37
60
// Write handles zerolog's json and sends events to sentry.
@@ -43,19 +66,23 @@ func (w *Writer) Write(data []byte) (n int, err error) {
43
66
return n , nil
44
67
}
45
68
46
- if _ , enabled := w .levels [lvl ]; ! enabled {
69
+ event , ok := w .parseLogEvent (data )
70
+ event .Level = levelsMapping [lvl ]
71
+
72
+ if ! ok {
47
73
return
48
74
}
49
75
50
- event , ok := w .parseLogEvent (data )
51
- event .Level = levelsMapping [lvl ]
76
+ if _ , enabled := w .levels [lvl ]; ! enabled {
77
+ // if the level is not enabled, add event as a breadcrumb
78
+ w .addBreadcrumb (event )
79
+ return
80
+ }
52
81
53
- if ok {
54
- w .hub .CaptureEvent (event )
55
- // should flush before os.Exit
56
- if event .Level == sentry .LevelFatal {
57
- w .hub .Flush (w .flushTimeout )
58
- }
82
+ w .hub .CaptureEvent (event )
83
+ // should flush before os.Exit
84
+ if event .Level == sentry .LevelFatal {
85
+ w .hub .Flush (w .flushTimeout )
59
86
}
60
87
61
88
return
@@ -188,6 +215,7 @@ type config struct {
188
215
environment string
189
216
serverName string
190
217
ignoreErrors []string
218
+ breadcrumbs bool
191
219
debug bool
192
220
tracing bool
193
221
debugWriter io.Writer
@@ -245,6 +273,13 @@ func WithIgnoreErrors(reList []string) WriterOption {
245
273
})
246
274
}
247
275
276
+ // WithBreadcrumbs enables sentry client breadcrumbs.
277
+ func WithBreadcrumbs () WriterOption {
278
+ return optionFunc (func (cfg * config ) {
279
+ cfg .breadcrumbs = true
280
+ })
281
+ }
282
+
248
283
// WithDebug enables sentry client debug logs.
249
284
func WithDebug () WriterOption {
250
285
return optionFunc (func (cfg * config ) {
@@ -350,9 +385,10 @@ func New(dsn string, opts ...WriterOption) (*Writer, error) {
350
385
}
351
386
352
387
return & Writer {
353
- hub : sentry .CurrentHub (),
354
- levels : levels ,
355
- flushTimeout : cfg .flushTimeout ,
388
+ hub : sentry .CurrentHub (),
389
+ levels : levels ,
390
+ flushTimeout : cfg .flushTimeout ,
391
+ withBreadcrumbs : cfg .breadcrumbs ,
356
392
}, nil
357
393
}
358
394
0 commit comments