user3389
user3389

Reputation: 489

How to integrate Sentry

I want to catch all errors in the production environment and send them to the Sentry. But I can't understand how to add it as a middleware. Do I need to write a custom logger than implement logger.Logger interface or I can do it somehow differently?

Upvotes: 0

Views: 2108

Answers (2)

user3389
user3389

Reputation: 489

Thanks, @martinni39 based on your code and code in the Buffalo manual I created this function:

func SentryLogger(lvl logger.Level) logger.FieldLogger {
l := logrus.New()
l.Level = lvl

levels := []logrus.Level{
    logrus.PanicLevel,
    logrus.FatalLevel,
    logrus.ErrorLevel,
}
hook, err := logrus_sentry.NewSentryHook("your sentry dsn", levels)
hook.StacktraceConfiguration.Enable = true
hook.StacktraceConfiguration.IncludeErrorBreadcrumb = true

if err == nil {
    l.Hooks.Add(hook)
}
return logger.Logrus{FieldLogger: l}
}

Then added to the buffalo options in the app.go

Logger:      SentryLogger(logger.DebugLevel),

Upvotes: 0

martinni39
martinni39

Reputation: 323

Seems like you want a sentry logging middleware. Every big logging library should have their own middleware implementation. For example logrus

import (
  "github.com/sirupsen/logrus"
  "github.com/evalphobia/logrus_sentry"
)

func main() {
  log       := logrus.New()
  hook, err := logrus_sentry.NewSentryHook(YOUR_DSN, []logrus.Level{
    logrus.PanicLevel,
    logrus.FatalLevel,
    logrus.ErrorLevel,
  })

  if err == nil {
    log.Hooks.Add(hook)
  }
}

If you want to create your own (assuming you're using logrus), you'll have to implement the interface for the hook and then post those entries yourself to sentry.

Upvotes: 2

Related Questions