From 10bed82eb3c0dab93f602e9a95c0573b8183ceff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emir=20Ribi=C4=87?= Date: Wed, 27 Nov 2024 17:54:59 +0100 Subject: [PATCH] add readme for slog integration (#905) * add readme for slog integration * partially revert converter * completely revert convertor --- slog/README.MD | 92 ++++++++++++++++++++++++++++++++++++++++++++++ slog/common.go | 2 +- slog/sentryslog.go | 1 - 3 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 slog/README.MD diff --git a/slog/README.MD b/slog/README.MD new file mode 100644 index 000000000..cc9d1e1c9 --- /dev/null +++ b/slog/README.MD @@ -0,0 +1,92 @@ +

+ + + +
+

+ +# 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 diff --git a/slog/common.go b/slog/common.go index afa74b7fa..8fd63a344 100644 --- a/slog/common.go +++ b/slog/common.go @@ -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 diff --git a/slog/sentryslog.go b/slog/sentryslog.go index 26538a6c1..362050f36 100644 --- a/slog/sentryslog.go +++ b/slog/sentryslog.go @@ -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