Skip to content

Commit e639797

Browse files
authored
fix(go): Proper slog levels (#14130)
1 parent ffadd7d commit e639797

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

docs/platforms/go/guides/logrus/index.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ For a quick reference, there is a [complete example](https://github.com/getsentr
77

88
Go API documentation for the [`sentrylogrus` package](https://pkg.go.dev/github.com/getsentry/sentry-go/logrus) is also available.
99

10+
## Requirements
11+
12+
Logrus structured logging is supported in Sentry Go SDK version `0.34.0` and above.
13+
1014
## Install
1115

1216
```bash

docs/platforms/go/guides/slog/index.mdx

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ For a quick reference, there is a [complete example](https://github.com/getsentr
77

88
Go API documentation for the [`sentryslog` package](https://pkg.go.dev/github.com/getsentry/sentry-go/slog) is also available.
99

10+
## Requirements
11+
12+
Slog structured logging is supported in Sentry Go SDK version `0.34.0` and above.
13+
1014
## Install
1115

1216
```bash
@@ -21,6 +25,7 @@ To integrate Sentry with slog, you need to set up a Sentry handler and configure
2125

2226
```go
2327
import (
28+
"context"
2429
"fmt"
2530
"log"
2631
"time"
@@ -49,8 +54,8 @@ func main() {
4954
// Configure `slog` to use Sentry as a handler
5055
ctx := context.Background()
5156
handler := sentryslog.Option{
52-
EventLevel: []slog.Level{slog.LevelError, sentryslog.LevelFatal}, // Only Error and Fatal as events
53-
LogLevel: []slog.Level{slog.LevelWarn, slog.LevelInfo}, // Only Warn and Info as logs
57+
EventLevel: []slog.Level{slog.LevelError}, // Only Error level will be sent as events
58+
LogLevel: []slog.Level{slog.LevelWarn, slog.LevelInfo}, // Only Warn and Info levels will be sent as logs
5459
}.NewSentryHandler(ctx)
5560
logger := slog.New(handler)
5661
logger = logger.With("release", "v1.0.0")
@@ -110,27 +115,40 @@ Sentry allows you to send logs either as log entries or as events. The minimum l
110115
### Example: Sending Logs as Events
111116

112117
```go
113-
logger := slog.New(sentryslog.Options
114-
EventLevel: []slog.Level{slog.LevelError, sentryslog.LevelFatal},
118+
ctx := context.Background()
119+
logger := slog.New(sentryslog.Option{
120+
// specify all event levels
121+
EventLevel: []slog.Level{
122+
slog.LevelDebug,
123+
slog.LevelInfo,
124+
slog.LevelWarn,
125+
slog.LevelError,
126+
},
115127
LogLevel: []slog.Level{}, // disable log entries
116128
AttrFromContext: []func(ctx context.Context) []slog.Attr{
117129
func(ctx context.Context) []slog.Attr {
118130
return []slog.Attr{slog.String("request_id", "123")}
119131
},
120132
},
121-
}.NewSentryHandler())
133+
}.NewSentryHandler(ctx))
122134

123135
logger.Error("This log is sent as an event")
124136
```
125137

126138
### Example: Sending Logs as Logs
127139

128140
```go
129-
logger := slog.New(sentryslog.Options{
130-
EventLevel: []slog.Level{}, // disable events
131-
LogLevel: []slog.Level{slog.LevelError},
132-
},
133-
}.NewSentryHandler())
141+
ctx := context.Background()
142+
logger := slog.New(sentryslog.Option{
143+
EventLevel: []slog.Level{}, // disable events
144+
// specify all log levels
145+
LogLevel: []slog.Level{
146+
slog.LevelDebug,
147+
slog.LevelInfo,
148+
slog.LevelWarn,
149+
slog.LevelError,
150+
},
151+
}.NewSentryHandler(ctx))
134152

135153
logger.Error("This log is sent as a log")
136154
```
@@ -154,14 +172,16 @@ func myAsyncHandler(w http.ResponseWriter, r *http.Request) {
154172
// The sentryhttp middleware adds a Hub with transaction information to the request context.
155173
ctx := r.Context()
156174
// By using InfoContext, the log entry will be associated with the transaction from the request.
157-
logger.InfoContext(ctx, "Log inside handler")
175+
slog.InfoContext(ctx, "Log inside handler")
158176
w.WriteHeader(http.StatusOK)
159177
fmt.Fprintln(w, "Handler finished, async task running in background.")
160178
}
161179

162-
// Wrap your handler with sentryhttp to automatically start transactions for requests.
163-
sentryHandler := sentryhttp.New(sentryhttp.Options{})
164-
http.Handle("/async", sentryHandler.Handle(http.HandlerFunc(myAsyncHandler)))
180+
func main() {
181+
// Wrap your handler with sentryhttp to automatically start transactions for requests.
182+
sentryHandler := sentryhttp.New(sentryhttp.Options{})
183+
http.Handle("/async", sentryHandler.Handle(http.HandlerFunc(myAsyncHandler)))
184+
}
165185
```
166186

167187
Note: Ensure Sentry is flushed before the application exits to avoid losing any pending events.

0 commit comments

Comments
 (0)