---
title: "Slog"
description: "Integrate slog with Sentry to capture and send structured logs."
url: https://docs.sentry.io/platforms/go/guides/http/logs/slog/
---

# Slog | Sentry for net/http

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.

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

## [Requirements](https://docs.sentry.io/platforms/go/guides/http/logs/slog.md#requirements)

Slog structured logging is supported in Sentry Go SDK version `0.34.0` and above.

## [Install](https://docs.sentry.io/platforms/go/guides/http/logs/slog.md#install)

```bash
go get github.com/getsentry/sentry-go
go get github.com/getsentry/sentry-go/slog
```

## [Configure](https://docs.sentry.io/platforms/go/guides/http/logs/slog.md#configure)

### [Initialize the Sentry SDK](https://docs.sentry.io/platforms/go/guides/http/logs/slog.md#initialize-the-sentry-sdk)

Error Monitoring\[ ]Tracing

```go
err := sentry.Init(sentry.ClientOptions{
    Dsn: "___PUBLIC_DSN___",
    // Enable printing of SDK debug messages.
    // Useful when getting started or trying to figure something out.
    Debug: true,
    // Adds request headers and IP for users,
    // visit: https://docs.sentry.io/platforms/go/data-management/data-collected/ for more info
    SendDefaultPII: true,
    EnableLogs: true,
    // ___PRODUCT_OPTION_START___ performance
    EnableTracing: true,
    // Set TracesSampleRate to 1.0 to capture 100%
    // of transactions for tracing.
    TracesSampleRate: 1.0,
    // ___PRODUCT_OPTION_END___ performance
})
if err != nil {
    log.Fatalf("sentry.Init: %s", err)
}
// Flush buffered events before the program terminates.
// Set the timeout to the maximum duration the program can afford to wait.
defer sentry.Flush(2 * time.Second)
```

### [Options](https://docs.sentry.io/platforms/go/guides/http/logs/slog.md#options)

`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:

```go
type Option struct {
	// EventLevel specifies the exact log levels to capture and send to Sentry as Events.
	// Only logs at these specific levels will be processed as events.
	// Defaults to []slog.Level{slog.LevelError, LevelFatal}.
	EventLevel []slog.Level

	// LogLevel specifies the exact log levels to capture and send to Sentry as Log entries.
	// Only logs at these specific levels will be processed as log entries.
	// Defaults to []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError, LevelFatal}.
	LogLevel []slog.Level

	// Hub specifies the Sentry Hub to use for capturing events.
	// If not provided, the current Hub is used by default.
	Hub *sentry.Hub

	// Converter is an optional function that customizes how log records
	// are converted into Sentry events. By default, the DefaultConverter is used.
	Converter Converter

	// AttrFromContext is an optional slice of functions that extract attributes
	// from the context. These functions can add additional metadata to the log entry.
	AttrFromContext []func(ctx context.Context) []slog.Attr

	// AddSource is an optional flag that, when set to true, includes the source
	// information (such as file and line number) in the Sentry event.
	// This can be useful for debugging purposes.
	AddSource bool

	// ReplaceAttr is an optional function that allows for the modification or
	// replacement of attributes in the log record. This can be used to filter
	// or transform attributes before they are sent to Sentry.
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}
```

## [Verify](https://docs.sentry.io/platforms/go/guides/http/logs/slog.md#verify)

```go
// Configure `slog` to use Sentry as a handler
ctx := context.Background()
handler := sentryslog.Option{
    // Explicitly specify the levels that you want to be captured.
	EventLevel: []slog.Level{slog.LevelError}, // Captures only [slog.LevelError] as error events.
	LogLevel:   []slog.Level{slog.LevelWarn, slog.LevelInfo}, // Captures only [slog.LevelWarn] and [slog.LevelInfo] as log entries.
}.NewSentryHandler(ctx)
logger := slog.New(handler)

// Send an error event to Sentry to verify functionality
logger.Error("This error will be sent to Sentry!")

// Also a log entry
logger.With("key.string", "value").Info("An error occurred")
```

## [Correlating Logs With Traces](https://docs.sentry.io/platforms/go/guides/http/logs/slog.md#correlating-logs-with-traces)

In order to properly attach the correct trace with each log entry, a `context.Context` is required. If you're using logs combined with tracing, you should pass the correct context to properly attach each trace with the appropriate logs.
