0

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user3389
  • 479
  • 5
  • 10
  • 1
    Have you tried looking at the documentation? https://gobuffalo.io/en/docs/middleware#writing-your-own-middleware https://docs.sentry.io/platforms/go/guides/http/ – Gavin Jan 26 '21 at 20:05
  • I found this one https://github.com/stnguyen90/sentry-go/blob/buffalo-integration/buffalo/sentrybuffalo.go on the official sentry-go GitHub. It's not merged in the master but I want to try it tomorrow. – user3389 Jan 26 '21 at 21:26

2 Answers2

2

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.

martinni39
  • 323
  • 3
  • 10
0

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),
user3389
  • 479
  • 5
  • 10