Skip to content

Commit

Permalink
Add (Go) Logrus documentation (#10974)
Browse files Browse the repository at this point in the history
  • Loading branch information
ribice authored and Zylphrex committed Sep 4, 2024
1 parent f8f938c commit 3156a17
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
92 changes: 92 additions & 0 deletions docs/platforms/go/guides/logrus/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
title: Logrus
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."
---

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.

Go API documentation for the [`sentrylogrus` package](https://pkg.go.dev/github.com/getsentry/sentry-go/logrus) is also available.

## Install

<OnboardingOptionButtons
options={[
'error-monitoring',
]}
/>

```bash
go get go get github.com/getsentry/sentry-go/logrus
```

<Break />

<SignInNote />

```go {"onboardingOptions": {"performance": "14-17"}}
import (
"fmt"
"net/http"
"os"
"time"

"github.com/sirupsen/logrus"

"github.com/getsentry/sentry-go"
sentrylogrus "github.com/getsentry/sentry-go/logrus"
)

logger := logrus.New()

// Log DEBUG and higher level logs to STDERR
logger.Level = logrus.DebugLevel
logger.Out = os.Stderr

// Send only ERROR and higher level logs to Sentry
sentryLevels := []logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel}

// Initialize Sentry
sentryHook, err := sentrylogrus.New(sentryLevels, sentry.ClientOptions{
Dsn: "___PUBLIC_DSN___",
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
if hint.Context != nil {
if req, ok := hint.Context.Value(sentry.RequestContextKey).(*http.Request); ok {
// Access the original Request here
fmt.Println(req)
}
}
fmt.Println(event)
return event
},
Debug: true,
AttachStacktrace: true,
})
if err != nil {
panic(err)
}
defer sentryHook.Flush(5 * time.Second)
logger.AddHook(sentryHook)

// Flushes before calling os.Exit(1) when using logger.Fatal
// (else all defers are not called, and Sentry does not have time to send the event)
logrus.RegisterExitHandler(func() { sentryHook.Flush(5 * time.Second) })

// Log an InfoLevel entry to STDERR (not sent to Sentry)
logger.Infof("Application has started")

// Log an ErrorLevel entry to STDERR and Sentry
logger.Errorf("oh no!")

// Log a FatalLevel entry to STDERR, send to Sentry, and terminate the application
logger.Fatalf("can't continue...")

```

## Configure

`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.

## Usage

Use `logrus` as you normally would, and it will automatically send logs at or above the specified levels to Sentry.

1 change: 1 addition & 0 deletions src/components/platformIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ export const PLATFORM_TO_ICON = {
'go-iris': 'iris',
'go-martini': 'martini',
'go-negroni': 'go',
'go-logrus': 'go',
godot: 'godot',
huggingface: 'huggingface',
java: 'java',
Expand Down
4 changes: 4 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ const USER_DOCS_REDIRECTS: Redirect[] = [
from: '/platforms/go/negroni/',
to: '/platforms/go/guides/negroni/',
},
{
from: '/platforms/go/logrus/',
to: '/platforms/go/guides/logrus/',
},
{
from: '/clients/go/integrations/http/',
to: '/platforms/go/guides/http/',
Expand Down

0 comments on commit 3156a17

Please sign in to comment.