Commit aea02cb1 authored by Conner Fromknecht's avatar Conner Fromknecht

feat: add SentryStreamHandler as drop-in log.Handler

Creates a custom SentryStremHandler that functions analgously to a
log.StreamHandler, with the exception that any LvlError logs or higher
are posted to Sentry. It also leverages the existing go-ethereum logging
formatters, specifically JSONFormat, to post any additional log context
items as a JSON blob via extra data.
parent 28a2e5d0
......@@ -4,6 +4,7 @@ go 1.16
require (
github.com/ethereum/go-ethereum v1.10.8
github.com/getsentry/sentry-go v0.11.0
github.com/stretchr/testify v1.7.0
github.com/urfave/cli v1.22.5
)
This diff is collapsed.
package batchsubmitter
import (
"errors"
"io"
"github.com/ethereum/go-ethereum/log"
"github.com/getsentry/sentry-go"
)
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.
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 {
sentry.WithScope(func(scope *sentry.Scope) {
scope.SetExtra("context", jsonFmt.Format(r))
sentry.CaptureException(errors.New(r.Msg))
})
}
return err
})
return log.LazyHandler(log.SyncHandler(h))
}
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