Skip to content

Commit 767f9ff

Browse files
committed
add slog documentation
1 parent abf7f1c commit 767f9ff

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
title: Slog
3+
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."
4+
---
5+
6+
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.
7+
8+
[Go Dev-style API documentation](https://pkg.go.dev/github.com/getsentry/sentry-go/slog) is also available.
9+
10+
## Install
11+
12+
<OnboardingOptionButtons
13+
options={[
14+
'error-monitoring',
15+
]}
16+
/>
17+
18+
```bash
19+
go get github.com/getsentry/sentry-go/slog
20+
```
21+
22+
<Break />
23+
24+
<SignInNote />
25+
26+
To integrate Sentry with slog, you need to set up a Sentry handler and configure the Sentry client.
27+
28+
```go
29+
{"onboardingOptions": {"performance": "12-16"}}
30+
import (
31+
"fmt"
32+
"log"
33+
"time"
34+
35+
"log/slog"
36+
37+
"github.com/getsentry/sentry-go"
38+
sentryslog "github.com/getsentry/sentry-go/slog"
39+
)
40+
41+
func main() {
42+
// Initialize Sentry
43+
err := sentry.Init(sentry.ClientOptions{
44+
Dsn: "___PUBLIC_DSN___",
45+
EnableTracing: false,
46+
})
47+
if err != nil {
48+
log.Fatal(err)
49+
}
50+
defer sentry.Flush(2 * time.Second)
51+
52+
// Configure `slog` to use Sentry as a handler
53+
logger := slog.New(sentryslog.Option{Level: slog.LevelDebug}.NewSentryHandler())
54+
logger = logger.With("release", "v1.0.0")
55+
56+
// Log messages with various attributes
57+
logger.
58+
With(
59+
slog.Group("user",
60+
slog.String("id", "user-123"),
61+
slog.Time("created_at", time.Now()),
62+
),
63+
).
64+
With("environment", "dev").
65+
With("error", fmt.Errorf("an error")).
66+
Error("a message")
67+
}
68+
```
69+
70+
## Configure
71+
72+
`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:
73+
74+
```go
75+
// Level sets the minimum log level to capture and send to Sentry.
76+
// Logs at this level and above will be processed. The default level is debug.
77+
Level slog.Leveler
78+
// Hub specifies the Sentry Hub to use for capturing events.
79+
// If not provided, the current Hub is used by default.
80+
Hub *sentry.Hub
81+
// Converter is an optional function that customizes how log records
82+
// are converted into Sentry events. By default, the DefaultConverter is used.
83+
Converter Converter
84+
// AttrFromContext is an optional slice of functions that extract attributes
85+
// from the context. These functions can add additional metadata to the log entry.
86+
AttrFromContext []func(ctx context.Context) []slog.Attr
87+
// AddSource is an optional flag that, when set to true, includes the source
88+
// information (such as file and line number) in the Sentry event.
89+
// This can be useful for debugging purposes.
90+
AddSource bool
91+
// ReplaceAttr is an optional function that allows for the modification or
92+
// replacement of attributes in the log record. This can be used to filter
93+
// or transform attributes before they are sent to Sentry.
94+
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
95+
```
96+
97+
## Usage
98+
99+
```go
100+
logger := slog.New(sentryslog.Option{
101+
Level: slog.LevelDebug,
102+
AttrFromContext: []func(ctx context.Context) []slog.Attr{
103+
func(ctx context.Context) []slog.Attr {
104+
return []slog.Attr{slog.String("request_id", "123")}
105+
},
106+
},
107+
}.NewSentryHandler())
108+
109+
logger = logger.With("release", "v1.0.0")
110+
111+
logger.
112+
With(
113+
slog.Group("user",
114+
slog.String("id", "user-123"),
115+
slog.Time("created_at", time.Now()),
116+
),
117+
).
118+
With("environment", "dev").
119+
With("error", fmt.Errorf("an error")).
120+
Error("a message")
121+
```
122+
123+
124+
125+
<SignInNote />
126+
127+
128+
Note: Ensure Sentry is flushed before the application exits to avoid losing any pending events.
129+

src/components/platformIcon.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ export const PLATFORM_TO_ICON = {
852852
'go-iris': 'iris',
853853
'go-martini': 'martini',
854854
'go-negroni': 'go',
855+
"go-slog": 'slog',
855856
godot: 'godot',
856857
huggingface: 'huggingface',
857858
java: 'java',
@@ -1040,12 +1041,12 @@ export function PlatformIcon({
10401041

10411042
if (withLanguageIcon && languageIcon !== icon && languageIcon !== 'default') {
10421043
return (
1043-
<div {...otherProps} style={{position: 'relative', ...style}}>
1044+
<div {...otherProps} style={{ position: 'relative', ...style }}>
10441045
<Image
10451046
src={svg}
10461047
width={size}
10471048
height={size}
1048-
style={{borderRadius: `${radius}px`}}
1049+
style={{ borderRadius: `${radius}px` }}
10491050
alt={`${platform} icon`}
10501051
loading="eager"
10511052
/>
@@ -1077,7 +1078,7 @@ export function PlatformIcon({
10771078
{...otherProps}
10781079
placeholder={undefined}
10791080
loading="eager"
1080-
style={{borderRadius: `${radius}px`, marginTop: 0, marginBottom: 0, ...style}}
1081+
style={{ borderRadius: `${radius}px`, marginTop: 0, marginBottom: 0, ...style }}
10811082
alt={`${platform} icon`}
10821083
/>
10831084
);

src/middleware.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ const USER_DOCS_REDIRECTS: Redirect[] = [
225225
from: '/platforms/go/negroni/',
226226
to: '/platforms/go/guides/negroni/',
227227
},
228+
{
229+
from: '/platforms/go/slog/',
230+
to: '/platforms/go/guides/slog/',
231+
},
228232
{
229233
from: '/clients/go/integrations/http/',
230234
to: '/platforms/go/guides/http/',

0 commit comments

Comments
 (0)