Skip to content

Commit ecd6c53

Browse files
committed
refactor logs pages to follow format
2 parents 6ee5de7 + d19bcfa commit ecd6c53

File tree

4 files changed

+199
-294
lines changed

4 files changed

+199
-294
lines changed

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

Lines changed: 123 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -18,172 +18,166 @@ go get github.com/getsentry/sentry-go
1818
go get github.com/getsentry/sentry-go/logrus
1919
```
2020

21-
<Break />
22-
2321
## Configure
2422

2523
### Initialize the Sentry SDK
2624

27-
<PlatformContent includePath="getting-started-config" />
25+
<PlatformContent includePath="getting-started-include-logs-config" />
2826

2927
### Options
3028

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

35-
#### LogHook
33+
## Verify
3634

37-
Use `sentrylogrus.NewLogHook()` to send structured logs to Sentry. This hook captures log entries and sends them to Sentry's structured logging system.
35+
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.
3836

37+
```go
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")
55+
}
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...")
96+
```
97+
98+
### LogHook
99+
100+
You have two ways to create a new `LogHook`. Either by using `sentrylogrus.NewLogHook()` and passing the `sentry.ClientOptions`, or
101+
by using `sentrylogrus.NewLogHookFromClient()` and passing an already created `sentry.Client`. These hook captures log entries and
102+
send them to Sentry's structured logging system.
103+
104+
#### NewLogHook
39105
```go
40106
logHook, err := sentrylogrus.NewLogHook(
41107
[]logrus.Level{logrus.InfoLevel, logrus.WarnLevel},
42108
sentry.ClientOptions{
43109
Dsn: "___PUBLIC_DSN___",
44110
EnableLogs: true, // Required for log entries
45-
})
111+
},
112+
)
113+
```
114+
115+
#### NewLogHookFromClient
116+
117+
Use `NewLogHookFromClient` if you've already initialized the Sentry SDK.
118+
```go
119+
if err := sentry.Init(sentry.ClientOptions{
120+
Dsn: "___PUBLIC_DSN___",
121+
EnableLogs: true,
122+
}); err != nil {
123+
log.Fatalf("Sentry initialization failed: %v", err)
124+
}
125+
hub := sentry.CurrentHub()
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+
}
46135
```
47136

48-
#### EventHook
137+
### EventHook
49138

50-
Use `sentrylogrus.NewEventHook()` to send log entries as Sentry events. This is useful for error tracking and alerting.
139+
You also have two ways to create a new `EventHook`. Either by using `sentrylogrus.NewEventHook()` and passing the `sentry.ClientOptions`, or
140+
by using `sentrylogrus.NewEventFromClient()` and passing an already created `sentry.Client`. These hook captures log entries and
141+
send them as events. This is helpful for error tracking and alerting.
51142

143+
#### NewEventHook
52144
```go
53145
eventHook, err := sentrylogrus.NewEventHook(
54146
[]logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel},
55147
sentry.ClientOptions{
56148
Dsn: "___PUBLIC_DSN___",
57149
Debug: true,
58150
AttachStacktrace: true,
59-
})
151+
},
152+
)
60153
```
154+
#### NewEventHookFromClient
61155

62-
<Alert>
63-
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.
64-
</Alert>
65-
66-
## Verify
67-
156+
Use `NewEventHookFromClient` if you've already initialized the Sentry SDK.
68157
```go
69-
// Initialize Logrus
70-
logger := logrus.New()
71-
logger.Level = logrus.DebugLevel
72-
logger.Out = os.Stderr
73-
74-
// Create event hook for errors
75-
eventHook, err := sentrylogrus.NewEventHook([]logrus.Level{
76-
logrus.ErrorLevel,
77-
logrus.FatalLevel,
78-
logrus.PanicLevel,
79-
}, sentry.ClientOptions{
80-
Dsn: "___PUBLIC_DSN___",
81-
AttachStacktrace: true,
82-
})
83-
if err != nil {
84-
panic(err)
158+
if err := sentry.Init(sentry.ClientOptions{
159+
Dsn: "https://[email protected]/0",
160+
EnableLogs: true,
161+
}); err != nil {
162+
log.Fatalf("Sentry initialization failed: %v", err)
163+
}
164+
hub := sentry.CurrentHub()
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")
85173
}
86-
defer eventHook.Flush(5 * time.Second)
87-
logger.AddHook(eventHook)
88-
89-
// Test logging - this will be sent to Sentry as an event
90-
logger.Error("This error will be sent to Sentry!")
91-
92-
// Example of logging with attributes
93-
logger.WithField("user", "test-user").Error("An error occurred")
94174
```
95175

96-
## Usage
97-
98-
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.
99-
100-
```go
101-
import (
102-
"fmt"
103-
"net/http"
104-
"os"
105-
"time"
106-
107-
"github.com/sirupsen/logrus"
108-
"github.com/getsentry/sentry-go"
109-
sentrylogrus "github.com/getsentry/sentry-go/logrus"
110-
)
176+
<Alert>
177+
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.
178+
</Alert>
111179

112-
func main() {
113-
// Initialize Logrus
114-
logger := logrus.New()
115-
116-
// Log DEBUG and higher level logs to STDERR
117-
logger.Level = logrus.DebugLevel
118-
logger.Out = os.Stderr
119-
120-
// send logs on InfoLevel
121-
logHook, err := sentrylogrus.NewLogHook(
122-
[]logrus.Level{logrus.InfoLevel},
123-
sentry.ClientOptions{
124-
Dsn: "___PUBLIC_DSN___",
125-
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
126-
if hint.Context != nil {
127-
if req, ok := hint.Context.Value(sentry.RequestContextKey).(*http.Request); ok {
128-
// You have access to the original Request
129-
fmt.Println(req)
130-
}
131-
}
132-
fmt.Println(event)
133-
return event
134-
},
135-
// need to have logs enabled
136-
EnableLogs: true,
137-
AttachStacktrace: true,
138-
})
139-
140-
// send events on Error, Fatal, Panic levels
141-
eventHook, err := sentrylogrus.NewEventHook([]logrus.Level{
142-
logrus.ErrorLevel,
143-
logrus.FatalLevel,
144-
logrus.PanicLevel,
145-
}, sentry.ClientOptions{
146-
Dsn: "___PUBLIC_DSN___",
147-
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
148-
if hint.Context != nil {
149-
if req, ok := hint.Context.Value(sentry.RequestContextKey).(*http.Request); ok {
150-
// You have access to the original Request
151-
fmt.Println(req)
152-
}
153-
}
154-
fmt.Println(event)
155-
return event
156-
},
157-
AttachStacktrace: true,
158-
})
159-
if err != nil {
160-
panic(err)
161-
}
162-
defer eventHook.Flush(5 * time.Second)
163-
defer logHook.Flush(5 * time.Second)
164-
logger.AddHook(eventHook)
165-
logger.AddHook(logHook)
166-
167-
// Flushes before calling os.Exit(1) when using logger.Fatal
168-
// (else all defers are not called, and Sentry does not have time to send the event)
169-
logrus.RegisterExitHandler(func() {
170-
eventHook.Flush(5 * time.Second)
171-
logHook.Flush(5 * time.Second)
172-
})
173-
174-
// Log a InfoLevel entry STDERR which is sent as a log to Sentry
175-
logger.Infof("Application has started")
176-
177-
// Log an error to STDERR which is also sent to Sentry
178-
logger.Errorf("oh no!")
179-
180-
// Log a fatal error to STDERR, which sends an event to Sentry and terminates the application
181-
logger.Fatalf("can't continue...")
182-
183-
// Example of logging with attributes
184-
logger.WithField("user", "test-user").Error("An error occurred")
185-
}
186-
```
180+
<Include name="logs/go-ctx-usage-alert.mdx"/>
187181

188182
## Logs
189183

0 commit comments

Comments
 (0)