-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter_decorator_sentry.go
79 lines (63 loc) · 1.97 KB
/
formatter_decorator_sentry.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
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xlog/blob/main/LICENSE.
package xlog
import (
"bytes"
"io"
"sync"
"github.com/getsentry/sentry-go"
)
var bufPool = sync.Pool{
New: func() any {
return new(bytes.Buffer)
},
}
// extractLevel searches for level label and returns its byte representation.
func extractLevel(labeledLevels map[string]Level, levelKey string, keyValues []any) Level {
if lvl, found := labeledLevels[extractKeyValue(levelKey, keyValues).(string)]; found {
return lvl
}
return LevelNone
}
// extractKeyValue searches for a key and returns its value.
func extractKeyValue(key string, keyValues []any) any {
for idx := 0; idx < len(keyValues); idx += 2 {
if keyValues[idx] == key && idx+1 < len(keyValues) {
return keyValues[idx+1]
}
}
return ""
}
// SentryFormatter is a decorator which sends another formatter 's output to Sentry.
// The writer from the Logger should be io.Discard, as it uses internally a bytes.Buffer.
var SentryFormatter = func(formatter Formatter, hub *sentry.Hub, opts *CommonOpts) Formatter {
var (
mu sync.Mutex
sentryLevelMap = map[Level]sentry.Level{
LevelDebug: sentry.LevelDebug,
LevelInfo: sentry.LevelInfo,
LevelWarning: sentry.LevelWarning,
LevelError: sentry.LevelError,
LevelCritical: sentry.LevelFatal,
LevelNone: sentry.Level(""),
}
labeledLevels = flipLevelLabels(opts.LevelLabels)
)
return func(_ io.Writer, keyValues []any) error {
keyValues = AppendNoValue(keyValues)
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)
if err := formatter(buf, keyValues); err != nil {
return err
}
sentryLevel := sentryLevelMap[extractLevel(labeledLevels, opts.LevelKey, keyValues)]
mu.Lock()
defer mu.Unlock()
hub.Scope().SetLevel(sentryLevel)
_ = hub.CaptureMessage(buf.String())
return nil
}
}