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 (
var jsonFmt = log.JSONFormat()
// SentryStreamHandler creates a log.Handler that behaves similarly to
// log.StreamHandler, however it writes any log levels greater than or equal to
// LvlError to Sentry. In that case, the passed log.Record is encoded using JSON
// rather than the default terminal output, so that it can be captured for
// debugging in the Sentry dashboard.
// log.StreamHandler, however it writes any log with severity greater than or
// equal to log.LvlError to Sentry. In that case, the passed log.Record is
// encoded using JSON rather than the default terminal output, so that it can be
// captured for debugging in the Sentry dashboard.
func SentryStreamHandler(wr io.Writer, fmtr log.Format) log.Handler {
h := log.FuncHandler(func(r *log.Record) error {
_, err := wr.Write(fmtr.Format(r))
// If this record is LvlError or higher, serialize the record
// using JSON and write it to Sentry. We also capture the error
// message separately so that it's easy to parse what the error
// is in the dashboard.
if r.Lvl >= log.LvlError {
// If this record's severity is log.LvlError or higher,
// serialize the record using JSON and write it to Sentry. We
// also capture the error message separately so that it's easy
// to parse what the error is in the dashboard.
//
// 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) {
scope.SetExtra("context", jsonFmt.Format(r))
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