Search code examples
gosentrybuffalo

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?


Solution

  • 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),