Commit 2960e402 authored by Conner Fromknecht's avatar Conner Fromknecht

fix: correct SentryStreamHandler to log Error and Crit

The constants are actually defined in the reverse order of their
severity. The prior behavior would log everything _but_ log.LvlCrit.
parent e87f2b3b
...@@ -11,18 +11,22 @@ import ( ...@@ -11,18 +11,22 @@ import (
var jsonFmt = log.JSONFormat() var jsonFmt = log.JSONFormat()
// SentryStreamHandler creates a log.Handler that behaves similarly to // SentryStreamHandler creates a log.Handler that behaves similarly to
// log.StreamHandler, however it writes any log levels greater than or equal to // log.StreamHandler, however it writes any log with severity greater than or
// LvlError to Sentry. In that case, the passed log.Record is encoded using JSON // equal to log.LvlError to Sentry. In that case, the passed log.Record is
// rather than the default terminal output, so that it can be captured for // encoded using JSON rather than the default terminal output, so that it can be
// debugging in the Sentry dashboard. // captured for debugging in the Sentry dashboard.
func SentryStreamHandler(wr io.Writer, fmtr log.Format) log.Handler { func SentryStreamHandler(wr io.Writer, fmtr log.Format) log.Handler {
h := log.FuncHandler(func(r *log.Record) error { h := log.FuncHandler(func(r *log.Record) error {
_, err := wr.Write(fmtr.Format(r)) _, err := wr.Write(fmtr.Format(r))
// If this record is LvlError or higher, serialize the record // If this record's severity is log.LvlError or higher,
// using JSON and write it to Sentry. We also capture the error // serialize the record using JSON and write it to Sentry. We
// message separately so that it's easy to parse what the error // also capture the error message separately so that it's easy
// is in the dashboard. // to parse what the error is in the dashboard.
if r.Lvl >= log.LvlError { //
// NOTE: The log.Lvl* constants are defined in reverse order of
// their severity, i.e. zero (log.LvlCrit) is the highest
// severity.
if r.Lvl <= log.LvlError {
sentry.WithScope(func(scope *sentry.Scope) { sentry.WithScope(func(scope *sentry.Scope) {
scope.SetExtra("context", jsonFmt.Format(r)) scope.SetExtra("context", jsonFmt.Format(r))
sentry.CaptureException(errors.New(r.Msg)) sentry.CaptureException(errors.New(r.Msg))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment