> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squasher.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Go slog Handler

> Send structured slog records to Squasher with an async handler for Go services.

Use the Go handler when your application already logs through `log/slog` and you want the same records to flow into Squasher.

## Installation

```bash theme={null}
go get github.com/squasher-ai/squasher/sdks/go
```

## Configuration

Create one handler with your Squasher credentials, then build a `slog.Logger` from it.

```go main.go theme={null}
package main

import (
	"log/slog"

	squasher "github.com/squasher-ai/squasher/sdks/go"
)

func main() {
	handler := squasher.NewHandler(
		"sq_pk_your_key",
		"your-project-id",
		squasher.WithLevel(slog.LevelInfo),
	)
	defer handler.Close()

	logger := slog.New(handler)
	logger.Info("service started")
}
```

| Option                | Type     | Default                      | Description                               |
| --------------------- | -------- | ---------------------------- | ----------------------------------------- |
| `WithEndpoint(...)`   | `Option` | `https://ingest.squasher.ai` | Override the ingest base URL              |
| `WithHTTPClient(...)` | `Option` | Default `http.Client`        | Provide a custom client or timeout policy |
| `WithBufferSize(...)` | `Option` | `100`                        | Control the async queue size              |
| `WithLevel(...)`      | `Option` | `slog.LevelDebug`            | Filter records before they are queued     |

## Usage Example

```go main.go theme={null}
package main

import (
	"context"
	"log/slog"

	squasher "github.com/squasher-ai/squasher/sdks/go"
)

func main() {
	handler := squasher.NewHandler("sq_pk_your_key", "your-project-id")
	defer handler.Close()

	logger := slog.New(handler)
	logger.Error(
		"checkout failed",
		slog.String("service", "api"),
		slog.Int("attempt", 2),
		slog.String("request_id", "req_123"),
	)

	_ = handler.Flush(context.Background())
}
```

The handler sends records asynchronously, so a final `Flush()` call is useful during shutdown paths, CLI commands, and tests.

## Custom Attributes

Use `WithAttrs()` for shared fields and `WithGroup()` for nested attributes that should stay together.

```go main.go theme={null}
package main

import (
	"log/slog"

	squasher "github.com/squasher-ai/squasher/sdks/go"
)

func main() {
	handler := squasher.NewHandler("sq_pk_your_key", "your-project-id")
	defer handler.Close()

	grouped := handler.WithAttrs([]slog.Attr{
		slog.String("service", "payments"),
	}).WithGroup("http")

	logger := slog.New(grouped)
	logger.Error(
		"request failed",
		slog.String("method", "POST"),
		slog.Int("status", 500),
	)
}
```

Those attributes are preserved in the structured payload that Squasher receives, which makes them available for filtering and debugging.

## Agent handoff

```text theme={null}
Install the Squasher Go SDK, add a slog handler with SQUASHER_API_KEY and SQUASHER_PROJECT_ID from env, preserve existing handlers if present, call Flush or Close during shutdown, and verify with one error-level log.
```
