|
| 1 | +--- |
| 2 | +title: Slog |
| 3 | +description: "Slog is a structured logging library for Go, introduced in Go 1.21. This guide demonstrates how to integrate slog with Sentry to capture and send logs to Sentry." |
| 4 | +--- |
| 5 | + |
| 6 | +For a quick reference, there is a [complete example](https://github.com/getsentry/sentry-go/tree/master/_examples/slog) at the Go SDK source code repository. |
| 7 | + |
| 8 | +Go API documentation for the [`sentryslog` package](https://pkg.go.dev/github.com/getsentry/sentry-go/slog) is also available. |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +<OnboardingOptionButtons |
| 13 | + options={[ |
| 14 | + 'error-monitoring', |
| 15 | + ]} |
| 16 | +/> |
| 17 | + |
| 18 | +```bash |
| 19 | +go get github.com/getsentry/sentry-go/slog |
| 20 | +``` |
| 21 | + |
| 22 | +<Break /> |
| 23 | + |
| 24 | +<SignInNote /> |
| 25 | + |
| 26 | +To integrate Sentry with slog, you need to set up a Sentry handler and configure the Sentry client. |
| 27 | + |
| 28 | +```go |
| 29 | +import ( |
| 30 | + "fmt" |
| 31 | + "log" |
| 32 | + "time" |
| 33 | + |
| 34 | + "log/slog" |
| 35 | + |
| 36 | + "github.com/getsentry/sentry-go" |
| 37 | + sentryslog "github.com/getsentry/sentry-go/slog" |
| 38 | +) |
| 39 | + |
| 40 | +func main() { |
| 41 | + // Initialize Sentry |
| 42 | + err := sentry.Init(sentry.ClientOptions{ |
| 43 | + Dsn: "___PUBLIC_DSN___", |
| 44 | + EnableTracing: false, |
| 45 | + }) |
| 46 | + if err != nil { |
| 47 | + log.Fatal(err) |
| 48 | + } |
| 49 | + defer sentry.Flush(2 * time.Second) |
| 50 | + |
| 51 | + // Configure `slog` to use Sentry as a handler |
| 52 | + logger := slog.New(sentryslog.Option{Level: slog.LevelDebug}.NewSentryHandler()) |
| 53 | + logger = logger.With("release", "v1.0.0") |
| 54 | + |
| 55 | + // Log messages with various attributes |
| 56 | + logger. |
| 57 | + With( |
| 58 | + slog.Group("user", |
| 59 | + slog.String("id", "user-123"), |
| 60 | + slog.Time("created_at", time.Now()), |
| 61 | + ), |
| 62 | + ). |
| 63 | + With("environment", "dev"). |
| 64 | + With("error", fmt.Errorf("an error")). |
| 65 | + Error("a message") |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Configure |
| 70 | + |
| 71 | +`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: |
| 72 | + |
| 73 | +```go |
| 74 | +// Level sets the minimum log level to capture and send to Sentry. |
| 75 | +// Logs at this level and above will be processed. The default level is debug. |
| 76 | +Level slog.Leveler |
| 77 | +// Hub specifies the Sentry Hub to use for capturing events. |
| 78 | +// If not provided, the current Hub is used by default. |
| 79 | +Hub *sentry.Hub |
| 80 | +// Converter is an optional function that customizes how log records |
| 81 | +// are converted into Sentry events. By default, the DefaultConverter is used. |
| 82 | +Converter Converter |
| 83 | +// AttrFromContext is an optional slice of functions that extract attributes |
| 84 | +// from the context. These functions can add additional metadata to the log entry. |
| 85 | +AttrFromContext []func(ctx context.Context) []slog.Attr |
| 86 | +// AddSource is an optional flag that, when set to true, includes the source |
| 87 | +// information (such as file and line number) in the Sentry event. |
| 88 | +// This can be useful for debugging purposes. |
| 89 | +AddSource bool |
| 90 | +// ReplaceAttr is an optional function that allows for the modification or |
| 91 | +// replacement of attributes in the log record. This can be used to filter |
| 92 | +// or transform attributes before they are sent to Sentry. |
| 93 | +ReplaceAttr func(groups []string, a slog.Attr) slog.Attr |
| 94 | +``` |
| 95 | + |
| 96 | +## Usage |
| 97 | + |
| 98 | +```go |
| 99 | +logger := slog.New(sentryslog.Option{ |
| 100 | + Level: slog.LevelDebug, |
| 101 | + AttrFromContext: []func(ctx context.Context) []slog.Attr{ |
| 102 | + func(ctx context.Context) []slog.Attr { |
| 103 | + return []slog.Attr{slog.String("request_id", "123")} |
| 104 | + }, |
| 105 | + }, |
| 106 | +}.NewSentryHandler()) |
| 107 | + |
| 108 | +logger = logger.With("release", "v1.0.0") |
| 109 | + |
| 110 | +logger. |
| 111 | + With( |
| 112 | + slog.Group("user", |
| 113 | + slog.String("id", "user-123"), |
| 114 | + slog.Time("created_at", time.Now()), |
| 115 | + ), |
| 116 | + ). |
| 117 | + With("environment", "dev"). |
| 118 | + With("error", fmt.Errorf("an error")). |
| 119 | + Error("a message") |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +<SignInNote /> |
| 125 | + |
| 126 | + |
| 127 | +Note: Ensure Sentry is flushed before the application exits to avoid losing any pending events. |
0 commit comments