-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentry.go
87 lines (78 loc) · 1.93 KB
/
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
80
81
82
83
84
85
86
87
package gologger
import (
"fmt"
"github.com/getsentry/sentry-go"
"github.com/pkg/errors"
"net/http"
)
type SentryLogger struct {
*AbstractLogger
hub *sentry.Hub
level Level
err error
}
func NewSentryLogger(hub *sentry.Hub, level Level) *SentryLogger {
a := &AbstractLogger{}
s := &SentryLogger{
a,
hub,
level,
nil,
}
a.Logger = s
return s
}
func (s *SentryLogger) Clone() *SentryLogger {
h := s.hub.Clone()
clone := NewSentryLogger(h, s.level)
clone.err = s.err
return clone
}
//func (s *SentryLogger) WithField(key string, value interface{}) Logger {
// return s.WithFields(Fields{key: value})
//}
func (s *SentryLogger) WithFields(fields Fields) Logger {
clone := s.Clone()
clone.hub.ConfigureScope(func(scope *sentry.Scope) {
scope.SetExtras(fields)
})
return clone
}
func (s *SentryLogger) WithError(err error) Logger {
clone := s.Clone()
clone.err = err
return clone
}
func (s *SentryLogger) WithRequest(req *http.Request) Logger {
clone := s.Clone()
clone.hub.ConfigureScope(func(scope *sentry.Scope) {
scope.SetRequest(req)
})
return clone
}
func (s *SentryLogger) Log(level Level, args ...interface{}) {
if level.isLower(s.level) {
return
}
message := fmt.Sprint(args...)
clone := s.Clone()
clone.hub.ConfigureScope(func(scope *sentry.Scope) {
scope.SetLevel(sentry.Level(level))
if clone.err != nil {
scope.AddEventProcessor(func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
err := clone.err
err = errors.Wrap(err, message)
event.Message = err.Error()
event.Level = sentry.Level(level)
//event.Fingerprint = append(event.Fingerprint, "{{ default }}", "{{ message }}", "{{ error.type }}", "{{ error.value }}")
//event.Fingerprint = []string{"{{ default }}", "{{ message }}", "{{ error.type }}", "{{ error.value }}"}
return event
})
}
})
if clone.err != nil {
clone.hub.CaptureException(clone.err)
} else {
clone.hub.CaptureMessage(message)
}
}