Skip to content

Commit 0cb3f24

Browse files
committed
follow base structure on logs
1 parent 57a499e commit 0cb3f24

File tree

2 files changed

+102
-140
lines changed

2 files changed

+102
-140
lines changed

docs/platforms/go/guides/logrus/index.mdx

Lines changed: 80 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -13,108 +13,86 @@ Logrus structured logging is supported in Sentry Go SDK version `0.34.0` and abo
1313

1414
## Install
1515

16-
Install the `sentrylogrus` package:
1716
```bash
17+
go get github.com/getsentry/sentry-go
1818
go get github.com/getsentry/sentry-go/logrus
1919
```
2020

2121
## Configure
2222

23-
To enable logging, you need to initialize the SDK with the `EnableLogs` option set to true.
23+
### Initialize the Sentry SDK
2424

25-
```go
26-
sentry.Init(sentry.ClientOptions{
27-
Dsn: "___PUBLIC_DSN___",
28-
// Enable logs to be sent to Sentry
29-
EnableLogs: true,
30-
})
31-
```
25+
<PlatformContent includePath="getting-started-config" />
3226

3327
### Options
3428

3529
`sentrylogrus` provides two types of hooks to configure the integration with Sentry. Both hooks accept these options:
3630
- **Levels**: A slice of `logrus.Level` specifying which log levels to capture
3731
- **ClientOptions**: Standard `sentry.ClientOptions` for configuration
3832

39-
## Usage
33+
## Verify
4034

4135
To integrate Sentry with Logrus, you can set up both log hooks and event hooks to capture different types of data at various log levels.
4236

4337
```go
44-
func main() {
45-
// Initialize Logrus
46-
logger := logrus.New()
47-
48-
// Log DEBUG and higher level logs to STDERR
49-
logger.Level = logrus.DebugLevel
50-
logger.Out = os.Stderr
51-
52-
// send logs on InfoLevel
53-
logHook, err := sentrylogrus.NewLogHook(
54-
[]logrus.Level{logrus.InfoLevel},
55-
sentry.ClientOptions{
56-
Dsn: "___PUBLIC_DSN___",
57-
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
58-
if hint.Context != nil {
59-
if req, ok := hint.Context.Value(sentry.RequestContextKey).(*http.Request); ok {
60-
// You have access to the original Request
61-
fmt.Println(req)
62-
}
63-
}
64-
fmt.Println(event)
65-
return event
66-
},
67-
// need to have logs enabled
68-
EnableLogs: true,
69-
AttachStacktrace: true,
70-
})
71-
72-
// send events on Error, Fatal, Panic levels
73-
eventHook, err := sentrylogrus.NewEventHook([]logrus.Level{
74-
logrus.ErrorLevel,
75-
logrus.FatalLevel,
76-
logrus.PanicLevel,
77-
}, sentry.ClientOptions{
78-
Dsn: "___PUBLIC_DSN___",
79-
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
80-
if hint.Context != nil {
81-
if req, ok := hint.Context.Value(sentry.RequestContextKey).(*http.Request); ok {
82-
// You have access to the original Request
83-
fmt.Println(req)
84-
}
85-
}
86-
fmt.Println(event)
87-
return event
88-
},
89-
AttachStacktrace: true,
90-
})
91-
if err != nil {
92-
panic(err)
93-
}
94-
defer eventHook.Flush(5 * time.Second)
95-
defer logHook.Flush(5 * time.Second)
96-
logger.AddHook(eventHook)
97-
logger.AddHook(logHook)
98-
99-
// Flushes before calling os.Exit(1) when using logger.Fatal
100-
// (else all defers are not called, and Sentry does not have time to send the event)
101-
logrus.RegisterExitHandler(func() {
102-
eventHook.Flush(5 * time.Second)
103-
logHook.Flush(5 * time.Second)
104-
})
105-
106-
// Log a InfoLevel entry STDERR which is sent as a log to Sentry
107-
logger.Infof("Application has started")
108-
109-
// Log an error to STDERR which is also sent to Sentry
110-
logger.Errorf("oh no!")
111-
112-
// Log a fatal error to STDERR, which sends an event to Sentry and terminates the application
113-
logger.Fatalf("can't continue...")
114-
115-
// Example of logging with attributes
116-
logger.WithField("user", "test-user").Error("An error occurred")
38+
// Initialize Sentry SDK
39+
if err := sentry.Init(sentry.ClientOptions{
40+
Dsn: "___PUBLIC_DSN___",
41+
EnableLogs: true,
42+
}); err != nil {
43+
log.Fatalf("Sentry initialization failed: %v", err)
44+
}
45+
46+
logger := logrus.New()
47+
logger.Level = logrus.DebugLevel
48+
logger.Out = os.Stderr
49+
50+
// Get the Sentry client from the current hub
51+
hub := sentry.CurrentHub()
52+
client := hub.Client()
53+
if client == nil {
54+
log.Fatalf("Sentry client is nil")
11755
}
56+
57+
// Create log hook to send logs on Info level
58+
logHook := sentrylogrus.NewLogHookFromClient(
59+
[]logrus.Level{logrus.InfoLevel},
60+
client,
61+
)
62+
63+
// Create event hook to send events on Error, Fatal, Panic levels
64+
eventHook := sentrylogrus.NewEventHookFromClient(
65+
[]logrus.Level{
66+
logrus.ErrorLevel,
67+
logrus.FatalLevel,
68+
logrus.PanicLevel,
69+
},
70+
client,
71+
)
72+
73+
defer eventHook.Flush(5 * time.Second)
74+
defer logHook.Flush(5 * time.Second)
75+
logger.AddHook(eventHook)
76+
logger.AddHook(logHook)
77+
78+
// Flushes before calling os.Exit(1) when using logger.Fatal
79+
// (else all defers are not called, and Sentry does not have time to send the event)
80+
logrus.RegisterExitHandler(func() {
81+
eventHook.Flush(5 * time.Second)
82+
logHook.Flush(5 * time.Second)
83+
})
84+
85+
// Info level is sent as a log to Sentry
86+
logger.Infof("Application has started")
87+
88+
// Example of logging with attributes
89+
logger.WithField("user", "test-user").Error("An error occurred")
90+
91+
// Error level is sent as an error event to Sentry
92+
logger.Errorf("oh no!")
93+
94+
// Fatal level is sent as an error event to Sentry and terminates the application
95+
logger.Fatalf("can't continue...")
11896
```
11997

12098
### LogHook
@@ -139,16 +117,21 @@ logHook, err := sentrylogrus.NewLogHook(
139117
Use `NewLogHookFromClient` if you've already initialized the Sentry SDK.
140118
```go
141119
if err := sentry.Init(sentry.ClientOptions{
142-
Dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
120+
Dsn: "___PUBLIC_DSN___",
143121
EnableLogs: true,
144122
}); err != nil {
145123
log.Fatalf("Sentry initialization failed: %v", err)
146124
}
147125
hub := sentry.CurrentHub()
148-
logHook := sentrylogrus.NewLogHookFromClient(
149-
[]logrus.Level{logrus.InfoLevel, logrus.WarnLevel},
150-
hub.Client(),
151-
)
126+
client := hub.Client()
127+
if client != nil {
128+
logHook := sentrylogrus.NewLogHookFromClient(
129+
[]logrus.Level{logrus.InfoLevel, logrus.WarnLevel},
130+
client,
131+
)
132+
} else {
133+
log.Fatalf("Sentrylogrus initialization failed: nil client")
134+
}
152135
```
153136

154137
### EventHook
@@ -179,10 +162,15 @@ if err := sentry.Init(sentry.ClientOptions{
179162
log.Fatalf("Sentry initialization failed: %v", err)
180163
}
181164
hub := sentry.CurrentHub()
182-
eventHook := sentrylogrus.NewEventHookFromClient(
183-
[]logrus.Level{logrus.InfoLevel, logrus.WarnLevel},
184-
hub.Client(),
185-
)
165+
client := hub.Client()
166+
if client != nil {
167+
eventHook := sentrylogrus.NewEventHookFromClient(
168+
[]logrus.Level{logrus.InfoLevel, logrus.WarnLevel},
169+
client,
170+
)
171+
} else {
172+
log.Fatalf("Sentrylogrus initialization failed: nil client")
173+
}
186174
```
187175

188176
<Alert>

docs/platforms/go/guides/slog/index.mdx

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,20 @@ Go API documentation for the [`sentryslog` package](https://pkg.go.dev/github.co
1111

1212
Slog structured logging is supported in Sentry Go SDK version `0.34.0` and above.
1313

14-
## Setup
14+
## Install
1515

16-
Install the `sentryslog` package:
1716
```bash
17+
go get github.com/getsentry/sentry-go
1818
go get github.com/getsentry/sentry-go/slog
1919
```
2020

21-
To enable logging, you need to initialize the SDK with the `EnableLogs` option set to true.
21+
## Configure
2222

23-
```go
24-
sentry.Init(sentry.ClientOptions{
25-
Dsn: "___PUBLIC_DSN___",
26-
// Enable logs to be sent to Sentry
27-
EnableLogs: true,
28-
})
29-
```
23+
### Initialize the Sentry SDK
3024

31-
## Configure
25+
<PlatformContent includePath="getting-started-config" />
26+
27+
### Options
3228

3329
`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:
3430

@@ -68,45 +64,23 @@ type Option struct {
6864
}
6965
```
7066

71-
## Usage
72-
73-
To integrate Sentry with slog, you need to set up a Sentry handler and configure the Sentry client:
67+
## Verify
7468

7569
```go
76-
func main() {
77-
if err := sentry.Init(sentry.ClientOptions{
78-
Dsn: "___PUBLIC_DSN___",
79-
EnableLogs: true,
80-
}); err != nil {
81-
log.Fatalf("Sentry initialization failed: %v", err)
82-
}
83-
// Flush buffered events before the program terminates.
84-
// Set the timeout to the maximum duration the program can afford to wait.
85-
defer sentry.Flush(2 * time.Second)
86-
87-
ctx := context.Background()
88-
// Configure the sentryslog handler.
89-
// The handler lets you explicitly declare how to handle each log level.
90-
// You can handle logs as events or as log entries by specifying the `EventLevel` and `LogLevel` slices.
91-
handler := sentryslog.Option{
92-
EventLevel: []slog.Level{slog.LevelError}, // Only LevelError entries will be sent as events
93-
LogLevel: []slog.Level{slog.LevelWarn, slog.LevelInfo}, // Only LevelWarn and LevelInfo entries will be sent as logs
94-
}.NewSentryHandler(ctx)
95-
logger := slog.New(handler)
96-
logger = logger.With("release", "v1.0.0")
97-
98-
// Log messages with various attributes
99-
logger.
100-
With(
101-
slog.Group("user",
102-
slog.String("id", "user-123"),
103-
slog.Time("created_at", time.Now()),
104-
),
105-
).
106-
With("environment", "dev").
107-
With("error", fmt.Errorf("an error")).
108-
ErrorContext(ctx, "a message")
109-
}
70+
// Configure `slog` to use Sentry as a handler
71+
ctx := context.Background()
72+
handler := sentryslog.Option{
73+
// Explicitly specify the levels that you want to be captured.
74+
EventLevel: []slog.Level{slog.LevelError}, // Captures only [slog.LevelError] as error events.
75+
LogLevel: []slog.Level{slog.LevelWarn, slog.LevelInfo}, // Captures only [slog.LevelWarn] and [slog.LevelInfo] as log entries.
76+
}.NewSentryHandler(ctx)
77+
logger := slog.New(handler)
78+
79+
// Send an error event to Sentry to verify functionality
80+
logger.Error("This error will be sent to Sentry!")
81+
82+
// Also a log entry
83+
logger.With("key.string", "value").Info("An error occurred")
11084
```
11185

11286
<Include name="logs/go-ctx-usage-alert.mdx"/>

0 commit comments

Comments
 (0)