@@ -7,6 +7,10 @@ For a quick reference, there is a [complete example](https://github.com/getsentr
7
7
8
8
Go API documentation for the [ ` sentryslog ` package] ( https://pkg.go.dev/github.com/getsentry/sentry-go/slog ) is also available.
9
9
10
+ ## Requirements
11
+
12
+ Slog structured logging is supported in Sentry Go SDK version ` 0.34.0 ` and above.
13
+
10
14
## Install
11
15
12
16
``` bash
@@ -21,6 +25,7 @@ To integrate Sentry with slog, you need to set up a Sentry handler and configure
21
25
22
26
``` go
23
27
import (
28
+ " context"
24
29
" fmt"
25
30
" log"
26
31
" time"
@@ -49,8 +54,8 @@ func main() {
49
54
// Configure `slog` to use Sentry as a handler
50
55
ctx := context.Background ()
51
56
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
54
59
}.NewSentryHandler (ctx)
55
60
logger := slog.New (handler)
56
61
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
110
115
### Example: Sending Logs as Events
111
116
112
117
``` 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
+ },
115
127
LogLevel : []slog.Level {}, // disable log entries
116
128
AttrFromContext : []func (ctx context.Context ) []slog.Attr {
117
129
func (ctx context.Context ) []slog.Attr {
118
130
return []slog.Attr {slog.String (" request_id" , " 123" )}
119
131
},
120
132
},
121
- }.NewSentryHandler ())
133
+ }.NewSentryHandler (ctx ))
122
134
123
135
logger.Error (" This log is sent as an event" )
124
136
```
125
137
126
138
### Example: Sending Logs as Logs
127
139
128
140
``` 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))
134
152
135
153
logger.Error (" This log is sent as a log" )
136
154
```
@@ -154,14 +172,16 @@ func myAsyncHandler(w http.ResponseWriter, r *http.Request) {
154
172
// The sentryhttp middleware adds a Hub with transaction information to the request context.
155
173
ctx := r.Context ()
156
174
// 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" )
158
176
w.WriteHeader (http.StatusOK )
159
177
fmt.Fprintln (w, " Handler finished, async task running in background." )
160
178
}
161
179
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
+ }
165
185
```
166
186
167
187
Note: Ensure Sentry is flushed before the application exits to avoid losing any pending events.
0 commit comments