forked from getsentry/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
230 lines (206 loc) · 6.49 KB
/
log.go
File metadata and controls
230 lines (206 loc) · 6.49 KB
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
package sentry
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/getsentry/sentry-go/attribute"
)
type LogLevel string
const (
LogLevelTrace LogLevel = "trace"
LogLevelDebug LogLevel = "debug"
LogLevelInfo LogLevel = "info"
LogLevelWarn LogLevel = "warn"
LogLevelError LogLevel = "error"
LogLevelFatal LogLevel = "fatal"
)
const (
LogSeverityTrace int = 1
LogSeverityDebug int = 5
LogSeverityInfo int = 9
LogSeverityWarning int = 13
LogSeverityError int = 17
LogSeverityFatal int = 21
)
var mapTypesToStr = map[attribute.Type]string{
attribute.INVALID: "",
attribute.BOOL: "boolean",
attribute.INT64: "integer",
attribute.FLOAT64: "double",
attribute.STRING: "string",
}
type sentryLogger struct {
client *Client
attributes map[string]Attribute
}
// NewLogger returns a Logger that emits logs to Sentry. If logging is turned off, all logs get discarded.
func NewLogger(ctx context.Context) Logger {
var hub *Hub
hub = GetHubFromContext(ctx)
if hub == nil {
hub = CurrentHub()
}
client := hub.Client()
if client != nil && client.batchLogger != nil {
return &sentryLogger{client, make(map[string]Attribute)}
}
DebugLogger.Println("fallback to noopLogger: enableLogs disabled")
return &noopLogger{} // fallback: does nothing
}
func (l *sentryLogger) Write(p []byte) (int, error) {
// Avoid sending double newlines to Sentry
msg := strings.TrimRight(string(p), "\n")
l.log(context.Background(), LogLevelInfo, LogSeverityInfo, msg)
return len(p), nil
}
func (l *sentryLogger) log(ctx context.Context, level LogLevel, severity int, message string, args ...interface{}) {
if message == "" {
return
}
hub := GetHubFromContext(ctx)
if hub == nil {
hub = CurrentHub()
}
var traceID TraceID
var spanID SpanID
span := hub.Scope().span
if span != nil {
traceID = span.TraceID
spanID = span.SpanID
} else {
traceID = hub.Scope().propagationContext.TraceID
}
attrs := map[string]Attribute{}
if len(args) > 0 {
attrs["sentry.message.template"] = Attribute{
Value: message, Type: "string",
}
for i, p := range args {
attrs[fmt.Sprintf("sentry.message.parameters.%d", i)] = Attribute{
Value: fmt.Sprint(p), Type: "string",
}
}
}
// If `log` was called with SetAttributes, pass the attributes to attrs
if len(l.attributes) > 0 {
for k, v := range l.attributes {
attrs[k] = v
}
// flush attributes from logger after send
clear(l.attributes)
}
// Set default attributes
if release := l.client.options.Release; release != "" {
attrs["sentry.release"] = Attribute{Value: release, Type: "string"}
}
if environment := l.client.options.Environment; environment != "" {
attrs["sentry.environment"] = Attribute{Value: environment, Type: "string"}
}
if serverName := l.client.options.ServerName; serverName != "" {
attrs["sentry.server.address"] = Attribute{Value: serverName, Type: "string"}
} else if serverAddr, err := os.Hostname(); err == nil {
attrs["sentry.server.address"] = Attribute{Value: serverAddr, Type: "string"}
}
scope := hub.Scope()
if scope != nil {
user := scope.user
if !user.IsEmpty() {
if user.ID != "" {
attrs["user.id"] = Attribute{Value: user.ID, Type: "string"}
}
if user.Name != "" {
attrs["user.name"] = Attribute{Value: user.Name, Type: "string"}
}
if user.Email != "" {
attrs["user.email"] = Attribute{Value: user.Email, Type: "string"}
}
}
}
if spanID.String() != "0000000000000000" {
attrs["sentry.trace.parent_span_id"] = Attribute{Value: spanID.String(), Type: "string"}
}
if sdkIdentifier := l.client.sdkIdentifier; sdkIdentifier != "" {
attrs["sentry.sdk.name"] = Attribute{Value: sdkIdentifier, Type: "string"}
}
if sdkVersion := l.client.sdkVersion; sdkVersion != "" {
attrs["sentry.sdk.version"] = Attribute{Value: sdkVersion, Type: "string"}
}
log := &Log{
Timestamp: time.Now(),
TraceID: traceID,
Level: level,
Severity: severity,
Body: fmt.Sprintf(message, args...),
Attributes: attrs,
}
if l.client.options.BeforeSendLog != nil {
log = l.client.options.BeforeSendLog(log)
}
if log != nil {
l.client.batchLogger.logCh <- *log
}
if l.client.options.Debug {
DebugLogger.Printf(message, args...)
}
}
func (l *sentryLogger) SetAttributes(attrs ...attribute.Builder) {
for _, v := range attrs {
t, ok := mapTypesToStr[v.Value.Type()]
if !ok || t == "" {
DebugLogger.Printf("invalid attribute type set: %v", t)
continue
}
l.attributes[v.Key] = Attribute{
Value: v.Value.AsInterface(),
Type: t,
}
}
}
func (l *sentryLogger) Trace(ctx context.Context, v ...interface{}) {
l.log(ctx, LogLevelTrace, LogSeverityTrace, fmt.Sprint(v...))
}
func (l *sentryLogger) Debug(ctx context.Context, v ...interface{}) {
l.log(ctx, LogLevelDebug, LogSeverityDebug, fmt.Sprint(v...))
}
func (l *sentryLogger) Info(ctx context.Context, v ...interface{}) {
l.log(ctx, LogLevelInfo, LogSeverityInfo, fmt.Sprint(v...))
}
func (l *sentryLogger) Warn(ctx context.Context, v ...interface{}) {
l.log(ctx, LogLevelWarn, LogSeverityWarning, fmt.Sprint(v...))
}
func (l *sentryLogger) Error(ctx context.Context, v ...interface{}) {
l.log(ctx, LogLevelError, LogSeverityError, fmt.Sprint(v...))
}
func (l *sentryLogger) Fatal(ctx context.Context, v ...interface{}) {
l.log(ctx, LogLevelFatal, LogSeverityFatal, fmt.Sprint(v...))
os.Exit(1)
}
func (l *sentryLogger) Panic(ctx context.Context, v ...interface{}) {
l.log(ctx, LogLevelFatal, LogSeverityFatal, fmt.Sprint(v...))
panic(fmt.Sprint(v...))
}
func (l *sentryLogger) Tracef(ctx context.Context, format string, v ...interface{}) {
l.log(ctx, LogLevelTrace, LogSeverityTrace, format, v...)
}
func (l *sentryLogger) Debugf(ctx context.Context, format string, v ...interface{}) {
l.log(ctx, LogLevelDebug, LogSeverityDebug, format, v...)
}
func (l *sentryLogger) Infof(ctx context.Context, format string, v ...interface{}) {
l.log(ctx, LogLevelInfo, LogSeverityInfo, format, v...)
}
func (l *sentryLogger) Warnf(ctx context.Context, format string, v ...interface{}) {
l.log(ctx, LogLevelWarn, LogSeverityWarning, format, v...)
}
func (l *sentryLogger) Errorf(ctx context.Context, format string, v ...interface{}) {
l.log(ctx, LogLevelError, LogSeverityError, format, v...)
}
func (l *sentryLogger) Fatalf(ctx context.Context, format string, v ...interface{}) {
l.log(ctx, LogLevelFatal, LogSeverityFatal, format, v...)
os.Exit(1)
}
func (l *sentryLogger) Panicf(ctx context.Context, format string, v ...interface{}) {
l.log(ctx, LogLevelFatal, LogSeverityFatal, format, v...)
panic(fmt.Sprint(v...))
}