|
| 1 | +--- |
| 2 | +title: Zerolog |
| 3 | +description: "Zerolog is a fast and efficient logging library for Go, designed for structured logging. This guide demonstrates how to integrate Zerolog with Sentry." |
| 4 | +--- |
| 5 | + |
| 6 | +For a complete example, visit the [Go SDK source code repository](https://github.com/getsentry/sentry-go/tree/master/_examples/zerolog). |
| 7 | + |
| 8 | +[Go Dev-style API documentation](https://pkg.go.dev/github.com/getsentry/sentry-go/zerolog) is also available. |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +<OnboardingOptionButtons |
| 13 | + options={[ |
| 14 | + 'error-monitoring', |
| 15 | + ]} |
| 16 | +/> |
| 17 | + |
| 18 | +```bash |
| 19 | +go get github.com/getsentry/sentry-go/zerolog |
| 20 | +``` |
| 21 | + |
| 22 | +<Break /> |
| 23 | + |
| 24 | +<SignInNote /> |
| 25 | + |
| 26 | +To integrate Sentry with Zerolog, you need to set up a custom writer that sends logs to Sentry based on the configured levels. |
| 27 | + |
| 28 | + |
| 29 | +```go {"onboardingOptions": {"performance": "12-16"}} |
| 30 | +import ( |
| 31 | + "errors" |
| 32 | + "time" |
| 33 | + "os" |
| 34 | + |
| 35 | + "github.com/getsentry/sentry-go" |
| 36 | + sentryzerolog "github.com/getsentry/sentry-go/zerolog" |
| 37 | + "github.com/rs/zerolog" |
| 38 | + "github.com/rs/zerolog/log" |
| 39 | +) |
| 40 | + |
| 41 | +func main() { |
| 42 | + // Initialize Sentry |
| 43 | + err := sentry.Init(sentry.ClientOptions{ |
| 44 | + Dsn: "___PUBLIC_DSN___", |
| 45 | + BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event { |
| 46 | + // Modify or filter events before sending them to Sentry |
| 47 | + return event |
| 48 | + }, |
| 49 | + Debug: true, |
| 50 | + AttachStacktrace: true, |
| 51 | + }) |
| 52 | + if err != nil { |
| 53 | + log.Fatal().Err(err).Msg("sentry initialization failed") |
| 54 | + } |
| 55 | + defer sentry.Flush(2 * time.Second) |
| 56 | + |
| 57 | + // Configure Zerolog to use Sentry as a writer |
| 58 | + sentryWriter, err := sentryzerolog.New(sentryzerolog.Config{ |
| 59 | + ClientOptions: sentry.ClientOptions{ |
| 60 | + Dsn: "___PUBLIC_DSN___", |
| 61 | + }, |
| 62 | + Options: sentryzerolog.Options{ |
| 63 | + Levels: []zerolog.Level{zerolog.ErrorLevel, zerolog.FatalLevel, zerolog.PanicLevel}, |
| 64 | + WithBreadcrumbs: true, |
| 65 | + FlushTimeout: 3 * time.Second, |
| 66 | + }, |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + log.Fatal().Err(err).Msg("failed to create sentry writer") |
| 70 | + } |
| 71 | + defer sentryWriter.Close() |
| 72 | + |
| 73 | + // Use Sentry writer in Zerolog |
| 74 | + log.Logger = log.Output(zerolog.MultiLevelWriter(zerolog.ConsoleWriter{Out: os.Stderr}, sentryWriter)) |
| 75 | + |
| 76 | + // Log an InfoLevel entry to STDERR (not sent to Sentry) |
| 77 | + log.Info().Msg("Application has started") |
| 78 | + |
| 79 | + // Log an ErrorLevel entry to STDERR and Sentry |
| 80 | + log.Error().Msg("oh no!") |
| 81 | + |
| 82 | + // Log a FatalLevel entry to STDERR, send to Sentry, and terminate the application |
| 83 | + log.Fatal().Err(errors.New("can't continue")).Msg("fatal error occurred") |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Configure |
| 88 | + |
| 89 | +sentryzerolog provides options to configure the integration with Sentry. It expects a `sentryzerolog.Config` that has `sentry.ClientOptions` and `sentryzerolog.Options`. The `sentry.ClientOptions` are used to initialize the Sentry client, and the `sentryzerolog.Options` are used to configure the Zerolog integration. |
| 90 | + |
| 91 | +The sentryzerolog.Options struct has the following fields: |
| 92 | + |
| 93 | + |
| 94 | +```go |
| 95 | +// Levels specifies the log levels that will trigger event sending to Sentry. |
| 96 | +// Only log messages at these levels will be sent. By default, the levels are |
| 97 | +// Error, Fatal, and Panic. |
| 98 | +Levels []zerolog.Level |
| 99 | + |
| 100 | +// WithBreadcrumbs, when enabled, adds log entries as breadcrumbs in Sentry. |
| 101 | +// Breadcrumbs provide a trail of events leading up to an error, which can |
| 102 | +// be invaluable for understanding the context of issues. |
| 103 | +WithBreadcrumbs bool |
| 104 | + |
| 105 | +// FlushTimeout sets the maximum duration allowed for flushing events to Sentry. |
| 106 | +// This is the time limit within which all pending events must be sent to Sentry |
| 107 | +// before the application exits. A typical use is ensuring all logs are sent before |
| 108 | +// application shutdown. The default timeout is usually 3 seconds. |
| 109 | +FlushTimeout time.Duration |
| 110 | +``` |
| 111 | + |
| 112 | +## Usage |
| 113 | + |
| 114 | +Use Zerolog as you normally would, and it will automatically send logs at or above the specified levels to Sentry. |
| 115 | + |
| 116 | +Note: Ensure Sentry is flushed before the application exits to avoid losing any pending events. |
0 commit comments