Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions docs/platforms/go/guides/zerolog/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---
title: Zerolog
description: "Zerolog is a fast and efficient logging library for Go, designed for structured logging. This guide demonstrates how to integrate Zerolog with Sentry."
---

For a complete example, visit the [Go SDK source code repository](https://github.com/getsentry/sentry-go/tree/master/_examples/zerolog).

[Go Dev-style API documentation](https://pkg.go.dev/github.com/getsentry/sentry-go/zerolog) is also available.

## Install

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

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

<Break />

<SignInNote />

To integrate Sentry with Zerolog, you need to set up a custom writer that sends logs to Sentry based on the configured levels.


```go {"onboardingOptions": {"performance": "12-16"}}
import (
"errors"
"time"
"os"

"github.com/getsentry/sentry-go"
sentryzerolog "github.com/getsentry/sentry-go/zerolog"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func main() {
// Initialize Sentry
err := sentry.Init(sentry.ClientOptions{
Dsn: "___PUBLIC_DSN___",
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
// Modify or filter events before sending them to Sentry
return event
},
Debug: true,
AttachStacktrace: true,
})
if err != nil {
log.Fatal().Err(err).Msg("sentry initialization failed")
}
defer sentry.Flush(2 * time.Second)

// Configure Zerolog to use Sentry as a writer
sentryWriter, err := sentryzerolog.New(sentryzerolog.Config{
ClientOptions: sentry.ClientOptions{
Dsn: "___PUBLIC_DSN___",
},
Options: sentryzerolog.Options{
Levels: []zerolog.Level{zerolog.ErrorLevel, zerolog.FatalLevel, zerolog.PanicLevel},
WithBreadcrumbs: true,
FlushTimeout: 3 * time.Second,
},
})
if err != nil {
log.Fatal().Err(err).Msg("failed to create sentry writer")
}
defer sentryWriter.Close()

// Use Sentry writer in Zerolog
log.Logger = log.Output(zerolog.MultiLevelWriter(zerolog.ConsoleWriter{Out: os.Stderr}, sentryWriter))

// Log an InfoLevel entry to STDERR (not sent to Sentry)
log.Info().Msg("Application has started")

// Log an ErrorLevel entry to STDERR and Sentry
log.Error().Msg("oh no!")

// Log a FatalLevel entry to STDERR, send to Sentry, and terminate the application
log.Fatal().Err(errors.New("can't continue")).Msg("fatal error occurred")
}
```

## Configure

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.

The sentryzerolog.Options struct has the following fields:


```go
// Levels specifies the log levels that will trigger event sending to Sentry.
// Only log messages at these levels will be sent. By default, the levels are
// Error, Fatal, and Panic.
Levels []zerolog.Level

// WithBreadcrumbs, when enabled, adds log entries as breadcrumbs in Sentry.
// Breadcrumbs provide a trail of events leading up to an error, which can
// be invaluable for understanding the context of issues.
WithBreadcrumbs bool

// FlushTimeout sets the maximum duration allowed for flushing events to Sentry.
// This is the time limit within which all pending events must be sent to Sentry
// before the application exits. A typical use is ensuring all logs are sent before
// application shutdown. The default timeout is usually 3 seconds.
FlushTimeout time.Duration
```

## Usage

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

Note: Ensure Sentry is flushed before the application exits to avoid losing any pending events.
1 change: 1 addition & 0 deletions src/components/platformIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,7 @@ export const PLATFORM_TO_ICON = {
'go-gin': 'gin',
'go-iris': 'iris',
'go-negroni': 'go',
'go-zerolog': 'go',
'go-slog': 'go',
'go-logrus': 'go',
godot: 'godot',
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/zerolog/',
to: '/platforms/go/guides/zerolog/',
},
{
from: '/platforms/go/slog/',
to: '/platforms/go/guides/slog/',
Expand Down
Loading