Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add readme for slog integration #905

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions slog/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<p align="center">
<a href="https://sentry.io" target="_blank" align="center">
<img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280">
</a>
<br />
</p>

# Official Sentry Integration for slog

**Go.dev Documentation:** https://pkg.go.dev/github.com/getsentry/sentry-go/sentryslog
**Example Usage:** https://github.com/getsentry/sentry-go/tree/master/_examples/slog

---

## Installation

```sh
go get github.com/getsentry/sentry-go/sentryslog
```

## Usage

```go
package main

import (
"context"
"log/slog"

"github.com/getsentry/sentry-go"
"github.com/your-repo/slog-sentry"
)

func main() {
// Initialize Sentry
err := sentry.Init(sentry.ClientOptions{
Dsn: "your-public-dsn",
Debug: true,
})
if err != nil {
panic(err)
}
defer sentry.Flush(5 * time.Second)

// Set up slog with Sentry handler
handler := slogSentry.Option{
Level: slog.LevelError, // Minimum log level
AddSource: true, // Include file/line source info
}.NewSentryHandler()
logger := slog.New(handler)

// Example logging
logger.Info("This will not be sent to Sentry")
logger.Error("An error occurred", "user", "test-user")
}
```

## Configuration

The slog-sentry package offers several options to customize how logs are handled and sent to Sentry. These are specified through the Option struct:

- `Level`: Minimum log level to send to Sentry. Defaults to `slog.LevelDebug`.

- `Hub`: Custom Sentry hub to use; defaults to the current Sentry hub if not set.

- `Converter`: Custom function to transform logs into Sentry events (default is DefaultConverter).

- `AttrFromContext`: Functions to extract additional attributes from the context.

- `AddSource`: Include file/line source info in Sentry events. Defaults to `false`.

- `ReplaceAttr`: Allows modification or filtering of attributes before sending to Sentry.


### Example Customization

```go
handler := slogSentry.Option{
Level: slog.LevelWarn,
Converter: func(addSource bool, replaceAttr func([]string, slog.Attr) slog.Attr, attrs []slog.Attr, groups []string, record *slog.Record, hub *sentry.Hub) *sentry.Event {
// Custom conversion logic
return &sentry.Event{
Message: record.Message,
}
},
AddSource: true,
}.NewSentryHandler()
```

## Notes

- Always call Flush to ensure all events are sent to Sentry before program termination
2 changes: 1 addition & 1 deletion slog/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func mergeAttrValues(values ...slog.Value) slog.Value {
}

func groupValuesByKey(attrs []slog.Attr) map[string][]slog.Value {
result := map[string][]slog.Value{}
result := make(map[string][]slog.Value)

for _, item := range attrs {
key := item.Key
Expand Down
1 change: 0 additions & 1 deletion slog/sentryslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

// Majority of the code in this package is derived from https://github.com/samber/slog-sentry AND https://github.com/samber/slog-common
// Smaller changes have been implemented to remove dependency on packages that are not available in the standard library of Go 1.18
// MIT License

// Copyright (c) 2023 Samuel Berthe
Expand Down
Loading