-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add readme for slog integration (#905)
* add readme for slog integration * partially revert converter * completely revert convertor
- Loading branch information
Showing
3 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters