You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We're actively working on adding more integration support for Logs. Currently we are looking at adding support for[`slog`](https://pkg.go.dev/log/slog), [`logrus`](https://pkg.go.dev/github.com/sirupsen/logrus), and[`zerolog`](https://pkg.go.dev/github.com/rs/zerolog). You can follow this [GitHub issue](https://github.com/getsentry/sentry-go/issues/1015) to track progress.
129
+
We're actively working on adding more integration support for Logs. Currently, we are looking at adding support for [`zerolog`](https://pkg.go.dev/github.com/rs/zerolog). You can follow this [GitHub issue](https://github.com/getsentry/sentry-go/issues/1015) to track progress.
Copy file name to clipboardExpand all lines: docs/platforms/go/common/usage/index.mdx
+4Lines changed: 4 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -32,3 +32,7 @@ Another common operation is to capture a bare message. A message is textual info
32
32
Messages show up as issues on your issue stream, with the message as the issue name.
33
33
34
34
<PlatformContentincludePath="capture-message" />
35
+
36
+
## Capturing Logs
37
+
38
+
Another common operation is to capture logs. Check <PlatformLinkto="/logs"> Logs </PlatformLink> for how to setup your logging integration to send log entries to Sentry.
description: "Logrus is a structured logger for Go, used to log messages in different formats and levels. This guide demonstrates how to integrate Logrus with Sentry to capture and send logs to Sentry."
3
+
description: "Logrus is a structured logger for Go, used to log messages in different formats and levels. This guide demonstrates how to integrate Logrus with Sentry to capture and send both logs and events to Sentry."
4
4
---
5
5
6
6
For a quick reference, there is a [complete example](https://github.com/getsentry/sentry-go/tree/master/_examples/logrus) at the Go SDK source code repository.
@@ -17,6 +17,8 @@ go get github.com/getsentry/sentry-go/logrus
17
17
18
18
<SignInNote />
19
19
20
+
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.
`sentrylogrus` allows configuration via the `New` function, which accepts the levels to log and `sentry.ClientOptions`. The levels to log are the logrus levels that should be sent to Sentry. The `sentry.ClientOptions` are the same as the ones used in the `sentry.Init` function.
112
+
`sentrylogrus` provides two types of hooks to configure the integration with Sentry. Both hooks accept these options:
113
+
-**Levels**: A slice of `logrus.Level` specifying which log levels to capture
114
+
-**ClientOptions**: Standard `sentry.ClientOptions` for configuration
115
+
116
+
### LogHook
117
+
118
+
Use `sentrylogrus.NewLogHook()` to send structured logs to Sentry. This hook captures log entries and sends them to Sentry's structured logging system.
When using both hooks, ensure you flush both of them before the application exits and register exit handlers for fatal logs to avoid losing pending events.
145
+
</Alert>
146
+
147
+
## Correlating Logs with Traces
148
+
149
+
To correlate logs with transactions, you need to pass a `context.Context` that contains transaction information to your logger calls. The `sentryhttp` middleware automatically adds transaction information to the request's context.
150
+
Here's an example of how to use `WithContext` in an HTTP handler to ensure logs are associated with the correct trace.
151
+
152
+
```go
153
+
// Assume logger is initialized and Sentry hooks are added as shown above.
154
+
// var logger *logrus.Logger
155
+
156
+
funcmyAsyncHandler(whttp.ResponseWriter, r *http.Request) {
157
+
// The sentryhttp middleware adds a Hub with transaction information to the request context.
158
+
ctx:= r.Context()
159
+
// By using WithContext, the log entry will be associated with the transaction from the request.
Use `logrus` as you normally would, and it will automatically send logs at or above the specified levels to Sentry.
173
+
For comprehensive logging setup with Logrus, including advanced configuration options and best practices, see the [Go Logs documentation](/platforms/go/logs/). The Logrus integration shown above provides seamless integration with Sentry's structured logging features.
EventLevel: []slog.Level{slog.LevelError, sentryslog.LevelFatal}, // Only Error and Fatal as events
53
+
LogLevel: []slog.Level{slog.LevelWarn, slog.LevelInfo}, // Only Warn and Info as logs
54
+
}.NewSentryHandler(ctx)
55
+
logger:= slog.New(handler)
50
56
logger = logger.With("release", "v1.0.0")
51
57
52
58
// Log messages with various attributes
@@ -59,7 +65,7 @@ func main() {
59
65
).
60
66
With("environment", "dev").
61
67
With("error", fmt.Errorf("an error")).
62
-
Error("a message")
68
+
ErrorContext(ctx, "a message")
63
69
}
64
70
```
65
71
@@ -68,9 +74,14 @@ func main() {
68
74
`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:
69
75
70
76
```go
71
-
// Level sets the minimum log level to capture and send to Sentry.
72
-
// Logs at this level and above will be processed. The default level is debug.
73
-
Level slog.Leveler
77
+
// EventLevel specifies the exact log levels to capture and send to Sentry as Events.
78
+
// Only logs at these specific levels will be processed as events.
79
+
// Defaults to []slog.Level{slog.LevelError, LevelFatal}.
80
+
EventLevel slog.Leveler
81
+
// LogLevel specifies the exact log levels to capture and send to Sentry as Log entries
82
+
// Only logs at these specific levels will be processed as log entries.
83
+
// Defaults to []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError, LevelFatal}.
84
+
LogLevel slog.Leveler
74
85
// Hub specifies the Sentry Hub to use for capturing events.
75
86
// If not provided, the current Hub is used by default.
76
87
Hub *sentry.Hub
@@ -92,33 +103,69 @@ ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
92
103
93
104
## Usage
94
105
106
+
### Sending Logs vs Events
107
+
108
+
Sentry allows you to send logs either as log entries or as events. The minimum log level defaults to `slog.LevelDebug` and logs will be sent if the `EnableLogs` option is set. The minimum event level defaults to `slog.LevelError`.
In order to properly attach the correct trace with each Log entry, a
140
+
`context.Context` is required. We recommend using `Context` functions to ensure your logs
141
+
are connected to spans and errors in the Sentry UI.
142
+
</Alert>
122
143
144
+
## Correlating Logs with Traces
145
+
146
+
To correlate logs with transactions, you need to pass a `context.Context` that contains transaction information to your logger calls. The `sentryhttp` middleware automatically adds transaction information to the request's context.
147
+
Here's an example of how to use `InfoContext` in an HTTP handler to ensure logs are associated with the correct trace.
148
+
149
+
```go
150
+
// Assume logger is initialized with the Sentry handler as shown above.
151
+
// var logger *slog.Logger
152
+
153
+
funcmyAsyncHandler(whttp.ResponseWriter, r *http.Request) {
154
+
// The sentryhttp middleware adds a Hub with transaction information to the request context.
155
+
ctx:= r.Context()
156
+
// By using InfoContext, the log entry will be associated with the transaction from the request.
157
+
logger.InfoContext(ctx, "Log inside handler")
158
+
w.WriteHeader(http.StatusOK)
159
+
fmt.Fprintln(w, "Handler finished, async task running in background.")
160
+
}
161
+
162
+
// Wrap your handler with sentryhttp to automatically start transactions for requests.
Note: Ensure Sentry is flushed before the application exits to avoid losing any pending events.
168
+
169
+
## Logs
170
+
171
+
For comprehensive logging setup with slog, including advanced configuration options and best practices, see the [Go Logs documentation](/platforms/go/logs/). The slog integration shown above provides seamless integration with Sentry's structured logging features.
0 commit comments