Skip to content

Commit 09c605b

Browse files
giortzisgclaude
andcommitted
docs(go): Fix slog examples
Replace the broken slog Option snippet with runnable examples and add a Go 1.25+ MultiHandler example so users can send logs to Sentry while keeping another handler active. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6fe10a2 commit 09c605b

1 file changed

Lines changed: 63 additions & 49 deletions

File tree

docs/platforms/go/common/logs/slog.mdx

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,61 +27,75 @@ go get github.com/getsentry/sentry-go/slog
2727

2828
### Options
2929

30-
`sentryslog` provides options to configure the integration with Sentry. It accepts a struct of `sentryslog.Options` that allows you to configure how the handler will behave. The options are:
30+
`sentryslog` accepts a `sentryslog.Option` struct to control which records are sent to Sentry and how they're enriched before sending.
31+
32+
| Field | Type | Description | Default |
33+
| ----------------- | ------------------------------------- | ----------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
34+
| `EventLevel` | `[]slog.Level` | Log levels to capture as Sentry events | `[]slog.Level{slog.LevelError, sentryslog.LevelFatal}` |
35+
| `LogLevel` | `[]slog.Level` | Log levels to capture as Sentry log entries | `[]slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError, sentryslog.LevelFatal}` |
36+
| `Hub` | `*sentry.Hub` | Hub to use when capturing events | Current hub |
37+
| `Converter` | `Converter` | Custom converter for turning log records into Sentry events | `sentryslog.DefaultConverter` |
38+
| `AttrFromContext` | `[]func(context.Context) []slog.Attr` | Functions that add attributes from the current context | None |
39+
| `AddSource` | `bool` | Include file and line information in Sentry output | `false` |
40+
| `ReplaceAttr` | `func([]string, slog.Attr) slog.Attr` | Rewrite or filter attributes before sending | None |
41+
42+
## Verify
43+
44+
This example sends `ERROR` records as events and `INFO`/`WARN` records as structured logs.
3145

3246
```go
33-
type Option struct {
34-
// EventLevel specifies the exact log levels to capture and send to Sentry as Events.
35-
// Only logs at these specific levels will be processed as events.
36-
// Defaults to []slog.Level{slog.LevelError, LevelFatal}.
37-
EventLevel []slog.Level
38-
39-
// LogLevel specifies the exact log levels to capture and send to Sentry as Log entries.
40-
// Only logs at these specific levels will be processed as log entries.
41-
// Defaults to []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError, LevelFatal}.
42-
LogLevel []slog.Level
43-
44-
// Hub specifies the Sentry Hub to use for capturing events.
45-
// If not provided, the current Hub is used by default.
46-
Hub *sentry.Hub
47-
48-
// Converter is an optional function that customizes how log records
49-
// are converted into Sentry events. By default, the DefaultConverter is used.
50-
Converter Converter
51-
52-
// AttrFromContext is an optional slice of functions that extract attributes
53-
// from the context. These functions can add additional metadata to the log entry.
54-
AttrFromContext []func(ctx context.Context) []slog.Attr
55-
56-
// AddSource is an optional flag that, when set to true, includes the source
57-
// information (such as file and line number) in the Sentry event.
58-
// This can be useful for debugging purposes.
59-
AddSource bool
60-
61-
// ReplaceAttr is an optional function that allows for the modification or
62-
// replacement of attributes in the log record. This can be used to filter
63-
// or transform attributes before they are sent to Sentry.
64-
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
47+
package main
48+
49+
import (
50+
"context"
51+
"log/slog"
52+
53+
sentryslog "github.com/getsentry/sentry-go/slog"
54+
)
55+
56+
func main() {
57+
ctx := context.Background()
58+
handler := sentryslog.Option{
59+
EventLevel: []slog.Level{slog.LevelError, sentryslog.LevelFatal},
60+
LogLevel: []slog.Level{slog.LevelInfo, slog.LevelWarn},
61+
AddSource: true,
62+
}.NewSentryHandler(ctx)
63+
64+
logger := slog.New(handler)
65+
66+
logger.Info("Application started", "service", "image-processor")
67+
logger.Warn("Cache miss", "key", "thumbnail:123")
68+
logger.Error("Image processing failed", "image_id", "img_123")
6569
}
6670
```
6771

68-
## Verify
72+
## Send Logs to Sentry and Another Handler
73+
74+
If you also want to keep writing logs somewhere else, Go 1.26+ includes `slog.NewMultiHandler`.
6975

7076
```go
71-
// Configure `slog` to use Sentry as a handler
72-
ctx := context.Background()
73-
handler := sentryslog.Option{
74-
// Explicitly specify the levels that you want to be captured.
75-
EventLevel: []slog.Level{slog.LevelError}, // Captures only [slog.LevelError] as error events.
76-
LogLevel: []slog.Level{slog.LevelWarn, slog.LevelInfo}, // Captures only [slog.LevelWarn] and [slog.LevelInfo] as log entries.
77-
}.NewSentryHandler(ctx)
78-
logger := slog.New(handler)
79-
80-
// Send an error event to Sentry to verify functionality
81-
logger.Error("This error will be sent to Sentry!")
82-
83-
// Also a log entry
84-
logger.With("key.string", "value").Info("An error occurred")
77+
package main
78+
79+
import (
80+
"context"
81+
"log/slog"
82+
"os"
83+
84+
sentryslog "github.com/getsentry/sentry-go/slog"
85+
)
86+
87+
func main() {
88+
ctx := context.Background()
89+
90+
sentryHandler := sentryslog.Option{
91+
LogLevel: []slog.Level{slog.LevelInfo, slog.LevelWarn, slog.LevelError},
92+
}.NewSentryHandler(ctx)
93+
94+
textHandler := slog.NewTextHandler(os.Stdout, nil)
95+
96+
logger := slog.New(slog.NewMultiHandler(sentryHandler, textHandler))
97+
logger.Info("Application started", "service", "image-processor")
98+
}
8599
```
86100

87-
<Include name="logs/go-ctx-usage-alert.mdx"/>
101+
<Include name="logs/go-ctx-usage-alert.mdx" />

0 commit comments

Comments
 (0)