sentryhook

package module
v0.0.0-...-e868e32 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2024 License: MIT Imports: 3 Imported by: 0

README

Sentry hook for Logrus logger

Sentry hook for Logrus logger.

Examples

Basic:

import (
  "github.com/sirupsen/logrus"
  "github.com/getsentry/sentry-go"

  "github.com/makasim/sentryhook"
)

func main() {
    if err := sentry.Init(sentry.ClientOptions{Dsn: "aDSN"}); err != nil {
        log.Fatal(err)
    }
    
    logger := logrus.New()
    logger.AddHook(sentryhook.New([]logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}))
    
    logger.Fatal("the error would be sent to sentry")
}

Customize Sentry event:

import (
  "github.com/sirupsen/logrus"
  "github.com/getsentry/sentry-go"

  "github.com/makasim/sentryhook"
)

func main() {
    if err := sentry.Init(sentry.ClientOptions{Dsn: "aDSN"}); err != nil {
        log.Fatal(err)
    }
    
    logger := logrus.New()
    logger.AddHook(sentryhook.New(
        []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel},
        sentryhook.WithConverter(newConverter()),
    ))
    
    logger.
        WithField("corr_id", "aCorrId").
        WithField("user_id", "aUserId").
        Fatal("the error would be sent to sentry")
}

func newConverter() sentryhook.Converter {
	return func(entry *logrus.Entry, hub *sentry.Hub) *sentry.Event {
		event := sentryhook.DefaultConverter(entry, hub)

		if pkg, ok := entry.Data["pkg"].(string); ok {
			event.Logger = pkg
		}

		if corrID, ok := entry.Data["corr_id"].(string); ok {
			event.Tags["corr_id"] = corrID
		}

		if userID, ok := entry.Data["user_id"]; ok {
			event.Tags["user_id"] = fmt.Sprintf("%v", userID)
		}

		return event
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultConverter

func DefaultConverter(entry *logrus.Entry, event *sentry.Event, hub *sentry.Hub)

Types

type Converter

type Converter func(entry *logrus.Entry, event *sentry.Event, hub *sentry.Hub)

type Hook

type Hook struct {
	// contains filtered or unexported fields
}

func New

func New(levels []logrus.Level, options ...Option) Hook

func (Hook) Fire

func (hook Hook) Fire(entry *logrus.Entry) error

func (Hook) Levels

func (hook Hook) Levels() []logrus.Level

type Option

type Option func(h *Hook)

func WithConverter

func WithConverter(c Converter) Option

func WithExtra

func WithExtra(extra map[string]interface{}) Option

func WithHub

func WithHub(hub *sentry.Hub) Option

func WithTags

func WithTags(tags map[string]string) Option

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL