# README
Official Sentry Logrus Hook for Sentry-go SDK
Go.dev Documentation: https://pkg.go.dev/github.com/getsentry/sentry-go/logrus
Example Usage: https://github.com/getsentry/sentry-go/tree/master/_examples/logrus
Installation
go get github.com/getsentry/sentry-go/logrus
Usage
import (
"fmt"
"os"
"time"
"github.com/sirupsen/logrus"
"github.com/getsentry/sentry-go"
sentrylogrus "github.com/getsentry/sentry-go/logrus"
)
func main() {
// Initialize Logrus
logger := logrus.New()
logger.Level = logrus.DebugLevel
logger.Out = os.Stderr
// Define log levels to send to Sentry
sentryLevels := []logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel}
// Initialize Sentry
sentryHook, err := sentrylogrus.New(sentryLevels, sentry.ClientOptions{
Dsn: "your-public-dsn",
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
fmt.Println(event) // Optional debugging
return event
},
Debug: true,
})
if err != nil {
panic(err)
}
defer sentryHook.Flush(5 * time.Second)
logger.AddHook(sentryHook)
// Example logging
logger.WithField("user", "test-user").Error("An error occurred")
// Ensure all events are flushed before exit
logrus.RegisterExitHandler(func() { sentryHook.Flush(5 * time.Second) })
}
Configuration
The sentrylogrus
package accepts an array of logrus.Level
and a struct of sentry.ClientOptions
that allows you to configure how the hook will behave.
The logrus.Level
array defines which log levels should be sent to Sentry.
In addition, the Hook returned by sentrylogrus.New
can be configured with the following options:
- Fallback Functionality: Configure a fallback for handling errors during log transmission.
sentryHook.Fallback = func(entry *logrus.Entry, err error) {
// Handle error
}
- Setting default tags for all events sent to Sentry
sentryHook.AddTags(map[string]string{
"key": "value",
})
Notes
- Always call Flush to ensure all events are sent to Sentry before program termination
# Functions
New initializes a new Logrus hook which sends logs to a new Sentry client configured according to opts.
NewFromClient initializes a new Logrus hook which sends logs to the provided sentry client.
# Constants
FieldFingerprint holds a string slice ([]string), used to dictate the grouping of this event.
These fields are simply omitted, as they are duplicated by the Sentry SDK.
These default log field keys are used to pass specific metadata in a way that Sentry understands.
FieldRequest holds an *http.Request.
FieldTransaction holds a transaction ID as a string.
FieldUser holds a User or *User value.
# Type aliases
A FallbackFunc can be used to attempt to handle any errors in logging, before resorting to Logrus's standard error reporting.