Repository navigation
Expand file tree
/
Copy pathlog.go
More file actions
321 lines (278 loc) · 7.04 KB
/
Copy pathlog.go
File metadata and controls
321 lines (278 loc) · 7.04 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package sentry
import (
"context"
"fmt"
"maps"
"os"
"strings"
"sync"
"time"
"github.com/getsentry/sentry-go/attribute"
"github.com/getsentry/sentry-go/internal/debuglog"
)
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
)
type sentryLogger struct {
ctx context.Context
attributes map[string]attribute.Value
mu sync.RWMutex
}
type logEntry struct {
logger *sentryLogger
ctx context.Context
level LogLevel
severity int
attributes map[string]attribute.Value
shouldPanic bool
shouldFatal bool
}
// NewLogger returns a Logger that emits logs to Sentry.
func NewLogger(ctx context.Context) Logger {
return &sentryLogger{ctx: ctx}
}
func (l *sentryLogger) Write(p []byte) (int, error) {
if !ClientFromContext(l.ctx).IsEnabled() {
return len(p), nil
}
msg := strings.TrimRight(string(p), "\n")
l.Info().Emit(msg)
return len(p), nil
}
func (l *sentryLogger) log(ctx context.Context, level LogLevel, severity int, message string, entryAttrs map[string]attribute.Value, format bool, args ...interface{}) {
if (format && message == "") || (!format && len(args) == 0) {
return
}
client := clientFromContexts(ctx, l.ctx)
if !client.IsEnabled() {
return
}
var body string
if format {
body = fmt.Sprintf(message, args...)
} else {
body = fmt.Sprint(args...)
if body == "" {
return
}
}
scope, fallbackCtx := scopeAndTraceFallback(ctx, l.ctx)
parameterAttributes := 0
if format {
parameterAttributes = len(args) + min(1, len(args))
}
l.mu.RLock()
attrs := mergeScopeAttributes(
client,
scope,
len(l.attributes)+len(entryAttrs)+parameterAttributes,
)
for k, v := range l.attributes {
attrs[k] = v
}
l.mu.RUnlock()
for k, v := range entryAttrs {
attrs[k] = v
}
if format && len(args) > 0 {
attrs["sentry.message.template"] = attribute.StringValue(message)
for i, p := range args {
attrs[fmt.Sprintf("sentry.message.parameters.%d", i)] = attribute.StringValue(fmt.Sprintf("%+v", p))
}
}
log := &Log{
Timestamp: time.Now(),
Level: level,
Severity: severity,
Body: body,
Attributes: attrs,
}
log.TraceID, log.SpanID = resolveTrace(scope, client, ctx, fallbackCtx)
log.approximateSize = computeLogSize(log)
client.captureLog(ctx, log)
if client.options.Debug {
debuglog.Print(body)
}
}
func (l *sentryLogger) SetAttributes(attrs ...attribute.Builder) {
l.mu.Lock()
defer l.mu.Unlock()
for _, a := range attrs {
if a.Value.Type() == attribute.INVALID {
debuglog.Printf("invalid attribute: %v", a)
continue
}
if l.attributes == nil {
l.attributes = make(map[string]attribute.Value, len(attrs))
}
l.attributes[a.Key] = a.Value
}
}
func (l *sentryLogger) Trace() LogEntry {
return &logEntry{
logger: l,
ctx: l.ctx,
level: LogLevelTrace,
severity: LogSeverityTrace,
attributes: make(map[string]attribute.Value),
}
}
func (l *sentryLogger) Debug() LogEntry {
return &logEntry{
logger: l,
ctx: l.ctx,
level: LogLevelDebug,
severity: LogSeverityDebug,
attributes: make(map[string]attribute.Value),
}
}
func (l *sentryLogger) Info() LogEntry {
return &logEntry{
logger: l,
ctx: l.ctx,
level: LogLevelInfo,
severity: LogSeverityInfo,
attributes: make(map[string]attribute.Value),
}
}
func (l *sentryLogger) Warn() LogEntry {
return &logEntry{
logger: l,
ctx: l.ctx,
level: LogLevelWarn,
severity: LogSeverityWarning,
attributes: make(map[string]attribute.Value),
}
}
func (l *sentryLogger) Error() LogEntry {
return &logEntry{
logger: l,
ctx: l.ctx,
level: LogLevelError,
severity: LogSeverityError,
attributes: make(map[string]attribute.Value),
}
}
func (l *sentryLogger) Fatal() LogEntry {
return &logEntry{
logger: l,
ctx: l.ctx,
level: LogLevelFatal,
severity: LogSeverityFatal,
attributes: make(map[string]attribute.Value),
shouldFatal: true,
}
}
func (l *sentryLogger) Panic() LogEntry {
return &logEntry{
logger: l,
ctx: l.ctx,
level: LogLevelFatal,
severity: LogSeverityFatal,
attributes: make(map[string]attribute.Value),
shouldPanic: true,
}
}
func (l *sentryLogger) LFatal() LogEntry {
return &logEntry{
logger: l,
ctx: l.ctx,
level: LogLevelFatal,
severity: LogSeverityFatal,
attributes: make(map[string]attribute.Value),
}
}
func (l *sentryLogger) GetCtx() context.Context {
return l.ctx
}
func (e *logEntry) WithCtx(ctx context.Context) LogEntry {
return &logEntry{
logger: e.logger,
ctx: ctx,
level: e.level,
severity: e.severity,
attributes: maps.Clone(e.attributes),
shouldPanic: e.shouldPanic,
shouldFatal: e.shouldFatal,
}
}
func (e *logEntry) String(key, value string) LogEntry {
e.attributes[key] = attribute.StringValue(value)
return e
}
func (e *logEntry) StringSlice(key string, value []string) LogEntry {
e.attributes[key] = attribute.StringSliceValue(value)
return e
}
func (e *logEntry) Int(key string, value int) LogEntry {
e.attributes[key] = attribute.Int64Value(int64(value))
return e
}
func (e *logEntry) Int64Slice(key string, value []int64) LogEntry {
e.attributes[key] = attribute.Int64SliceValue(value)
return e
}
func (e *logEntry) Int64(key string, value int64) LogEntry {
e.attributes[key] = attribute.Int64Value(value)
return e
}
func (e *logEntry) Float64Slice(key string, value []float64) LogEntry {
e.attributes[key] = attribute.Float64SliceValue(value)
return e
}
func (e *logEntry) Float64(key string, value float64) LogEntry {
e.attributes[key] = attribute.Float64Value(value)
return e
}
func (e *logEntry) BoolSlice(key string, value []bool) LogEntry {
e.attributes[key] = attribute.BoolSliceValue(value)
return e
}
func (e *logEntry) Bool(key string, value bool) LogEntry {
e.attributes[key] = attribute.BoolValue(value)
return e
}
// Uint64 adds uint64 attributes to the log entry.
//
// This method is intentionally not part of the LogEntry interface to avoid exposing uint64 in the public API.
func (e *logEntry) Uint64(key string, value uint64) LogEntry {
e.attributes[key] = attribute.Uint64Value(value)
return e
}
func (e *logEntry) Emit(args ...interface{}) {
e.logger.log(e.ctx, e.level, e.severity, "", e.attributes, false, args...)
if e.level == LogLevelFatal {
if e.shouldPanic {
panic(fmt.Sprint(args...))
}
if e.shouldFatal {
os.Exit(1)
}
}
}
func (e *logEntry) Emitf(format string, args ...interface{}) {
e.logger.log(e.ctx, e.level, e.severity, format, e.attributes, true, args...)
if e.level == LogLevelFatal {
if e.shouldPanic {
formattedMessage := fmt.Sprintf(format, args...)
panic(formattedMessage)
}
if e.shouldFatal {
os.Exit(1)
}
}
}