Skip to content

Commit

Permalink
add readme for slog integration (#905)
Browse files Browse the repository at this point in the history
* add readme for slog integration

* partially revert converter

* completely revert convertor
  • Loading branch information
ribice authored Nov 27, 2024
1 parent 297ccaf commit 10bed82
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
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

0 comments on commit 10bed82

Please sign in to comment.