Skip to content

Commit edf36a8

Browse files
committed
feat(go): add logs page
1 parent 1ccb14d commit edf36a8

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: Set Up Logs
3+
sidebar_title: Logs
4+
description: "Structured logs allow you to send, view and query logs sent from your applications within Sentry."
5+
sidebar_order: 5600
6+
---
7+
8+
<Include name="feature-stage-beta-logs.mdx" />
9+
10+
With Sentry Structured Logs, you can send text based log information from your applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes.
11+
12+
## Requirements
13+
14+
Logs in Go are supported in Sentry Go SDK version `0.33.0` and above.
15+
16+
## Setup
17+
18+
To enable logging, you need to initialize the SDK with the `EnableLogs` option set to true.
19+
20+
```go
21+
package main
22+
23+
import (
24+
"context"
25+
"github.com/getsentry/sentry-go"
26+
"time"
27+
)
28+
29+
func main() {
30+
if err := sentry.Init(sentry.ClientOptions{
31+
Dsn: "___PUBLIC_DSN___",
32+
EnableLogs: true,
33+
}); err != nil {
34+
fmt.Printf("Sentry initialization failed: %v\n", err)
35+
}
36+
// Flush buffered events before the program terminates.
37+
// Set the timeout to the maximum duration the program can afford to wait.
38+
defer sentry.Flush(2 * time.Second)
39+
}
40+
```
41+
42+
## Usage
43+
44+
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs by using
45+
the `sentry.Logger` API.
46+
47+
The `Sentry.Logger` API exposes methods that support six different log levels: `trace`,
48+
`debug`, `info`, `warn`, `error` and `fatal`. The methods support both `fmt.Print` and `fmt.Println` like syntax. Using
49+
these methods is as simple as:
50+
51+
```go
52+
ctx := context.Background()
53+
logger := sentry.NewLogger(ctx)
54+
55+
// You can use the logger like [fmt.Print]
56+
logger.Info(ctx, "Hello ", "world!")
57+
// Or like [fmt.Printf]
58+
logger.Infof(ctx, "Hello %v!", "world")
59+
```
60+
Additionally, you can attach properties to each log entry using the `attribute` API. These properties will be sent
61+
to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column.
62+
63+
```go
64+
package main
65+
66+
import (
67+
"context"
68+
"github.com/getsentry/sentry-go"
69+
"github.com/getsentry/sentry-go/attribute"
70+
"time"
71+
)
72+
73+
func main() {
74+
err := sentry.Init(sentry.ClientOptions{
75+
Dsn: "___PUBLIC_DSN___",
76+
EnableLogs: true,
77+
})
78+
if err != nil {
79+
panic(err)
80+
}
81+
defer sentry.Flush(2 * time.Second)
82+
83+
ctx := context.Background()
84+
logger := sentry.NewLogger(ctx)
85+
86+
logger.SetAttributes(
87+
attribute.Int("key.int", 42),
88+
attribute.Bool("key.boolean", true),
89+
attribute.Float64("key.float", 42.4),
90+
attribute.String("key.string", "string"),
91+
)
92+
logger.Warnf(ctx, "I have params: %v and attributes", "example param")
93+
}
94+
```
95+
Currently the `attribute` API supports only these value types: `int`, `string`, `bool` and `float`.
96+
97+
## Integrations
98+
99+
The `sentry.Logger` implements the `io.Writer` interface, so you can easily inject the logger into your existing setup.
100+
101+
```go
102+
sentryLogger := sentry.NewLogger(ctx)
103+
logger := log.New(sentryLogger, "", log.LstdFlags)
104+
logger.Println("Implementing log.Logger")
105+
106+
slogger := slog.New(slog.NewJSONHandler(sentryLogger, nil))
107+
slogger.Info("Implementing slog.Logger")
108+
```
109+
110+
<Alert>
111+
However in order to properly attach the correct Spans with each Log entry, a `context.Context` is required. The `Write` function of
112+
the `io.Writer` interface does not provide `context` so currently wrapping the custom logger will provide limited functionality.
113+
The recommended way is to use the `sentry.Logger` itself.
114+
</Alert>
115+
116+
## Options
117+
118+
### BeforeSendLog
119+
120+
To filter logs, or update them before they are sent to Sentry, you can use the `BeforeSendLog` client option.
121+
```go
122+
if err := sentry.Init(sentry.ClientOptions{
123+
Dsn: "___PUBLIC_DSN___",
124+
EnableLogs: true,
125+
BeforeSendLog: func(log *Log) *Log {
126+
// filter out all trace logs
127+
if log.Level == sentry.LogLevelTrace {
128+
return nil
129+
}
130+
131+
// filter all logs below warning
132+
if log.Severity <= LogSeverityInfo {
133+
return nil
134+
}
135+
},
136+
}); err != nil {
137+
fmt.Printf("Sentry initialization failed: %v\n", err)
138+
}
139+
```
140+
141+
### Debug
142+
143+
If debug is set to true, calls to the logger will also print to the console with the appropriate log level.

0 commit comments

Comments
 (0)