-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add (Go) Slog documentation #10976
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
Merged
Add (Go) Slog documentation #10976
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
767f9ff
add slog documentation
ribice 227cf2a
update icon
ribice 2959801
[getsentry/action-github-commit] Auto commit
getsantry[bot] 43e286a
fix slog
ribice 90d84ae
Update docs/platforms/go/guides/slog/index.mdx
ribice 1683571
merge master
ribice f928128
[getsentry/action-github-commit] Auto commit
getsantry[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,127 @@ | ||
| --- | ||
| title: Slog | ||
| description: "Slog is a structured logging library for Go, introduced in Go 1.21. This guide demonstrates how to integrate slog with Sentry to capture and send logs to Sentry." | ||
| --- | ||
|
|
||
| For a quick reference, there is a [complete example](https://github.com/getsentry/sentry-go/tree/master/_examples/slog) at the Go SDK source code repository. | ||
|
|
||
| Go API documentation for the [`sentryslog` package](https://pkg.go.dev/github.com/getsentry/sentry-go/slog) is also available. | ||
|
|
||
| ## Install | ||
|
|
||
| <OnboardingOptionButtons | ||
| options={[ | ||
| 'error-monitoring', | ||
| ]} | ||
| /> | ||
|
|
||
| ```bash | ||
| go get github.com/getsentry/sentry-go/slog | ||
| ``` | ||
|
|
||
| <Break /> | ||
|
|
||
| <SignInNote /> | ||
|
|
||
| To integrate Sentry with slog, you need to set up a Sentry handler and configure the Sentry client. | ||
|
|
||
| ```go | ||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "time" | ||
|
|
||
| "log/slog" | ||
|
|
||
| "github.com/getsentry/sentry-go" | ||
| sentryslog "github.com/getsentry/sentry-go/slog" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Initialize Sentry | ||
| err := sentry.Init(sentry.ClientOptions{ | ||
| Dsn: "___PUBLIC_DSN___", | ||
| EnableTracing: false, | ||
| }) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| defer sentry.Flush(2 * time.Second) | ||
|
|
||
| // Configure `slog` to use Sentry as a handler | ||
| logger := slog.New(sentryslog.Option{Level: slog.LevelDebug}.NewSentryHandler()) | ||
| logger = logger.With("release", "v1.0.0") | ||
|
|
||
| // Log messages with various attributes | ||
| logger. | ||
| With( | ||
| slog.Group("user", | ||
| slog.String("id", "user-123"), | ||
| slog.Time("created_at", time.Now()), | ||
| ), | ||
| ). | ||
| With("environment", "dev"). | ||
| With("error", fmt.Errorf("an error")). | ||
| Error("a message") | ||
| } | ||
| ``` | ||
|
|
||
| ## Configure | ||
|
|
||
| `sentryslog` provides options to configure the integration with Sentry. It accepts a struct of `sentryslog.Options` that allows you to configure how the handler will behave. The options are: | ||
|
|
||
| ```go | ||
| // Level sets the minimum log level to capture and send to Sentry. | ||
| // Logs at this level and above will be processed. The default level is debug. | ||
| Level slog.Leveler | ||
| // Hub specifies the Sentry Hub to use for capturing events. | ||
| // If not provided, the current Hub is used by default. | ||
| Hub *sentry.Hub | ||
| // Converter is an optional function that customizes how log records | ||
| // are converted into Sentry events. By default, the DefaultConverter is used. | ||
| Converter Converter | ||
| // AttrFromContext is an optional slice of functions that extract attributes | ||
| // from the context. These functions can add additional metadata to the log entry. | ||
| AttrFromContext []func(ctx context.Context) []slog.Attr | ||
| // AddSource is an optional flag that, when set to true, includes the source | ||
| // information (such as file and line number) in the Sentry event. | ||
| // This can be useful for debugging purposes. | ||
| AddSource bool | ||
| // ReplaceAttr is an optional function that allows for the modification or | ||
| // replacement of attributes in the log record. This can be used to filter | ||
| // or transform attributes before they are sent to Sentry. | ||
| ReplaceAttr func(groups []string, a slog.Attr) slog.Attr | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```go | ||
| logger := slog.New(sentryslog.Option{ | ||
| Level: slog.LevelDebug, | ||
| AttrFromContext: []func(ctx context.Context) []slog.Attr{ | ||
| func(ctx context.Context) []slog.Attr { | ||
| return []slog.Attr{slog.String("request_id", "123")} | ||
| }, | ||
| }, | ||
| }.NewSentryHandler()) | ||
|
|
||
| logger = logger.With("release", "v1.0.0") | ||
|
|
||
| logger. | ||
| With( | ||
| slog.Group("user", | ||
| slog.String("id", "user-123"), | ||
| slog.Time("created_at", time.Now()), | ||
| ), | ||
| ). | ||
| With("environment", "dev"). | ||
| With("error", fmt.Errorf("an error")). | ||
| Error("a message") | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| <SignInNote /> | ||
|
|
||
|
|
||
| Note: Ensure Sentry is flushed before the application exits to avoid losing any pending events. | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.