Capturing logs
Honeybadger provides handlers for popular Go logging libraries that send structured logs directly to Honeybadger Insights as events.
Supported libraries
Section titled “Supported libraries”The slog handler sends logs from Go’s standard log/slog package to
Honeybadger Insights.
Requires Go 1.21+
Quick start
Section titled “Quick start”import ( "log/slog" "github.com/honeybadger-io/honeybadger-go" hbslog "github.com/honeybadger-io/honeybadger-go/slog")
func main() { client := honeybadger.New(honeybadger.Configuration{ APIKey: "PROJECT_API_KEY", }) logger := slog.New(hbslog.New(client)) logger.Info("app started", "version", "1.0.0")}This produces an event in Honeybadger Insights:
{ "event_type": "log", "level": "INFO", "message": "app started", "version": "1.0.0"}Event types
Section titled “Event types”The default event type is log. Set a custom event type for all logs using
WithEventType:
audit := slog.New(hbslog.New(client).WithEventType("audit"))audit.Info("user logged in", "user_id", 42)Set the event type per log call with the event_type attribute:
logger.Info("user signup", "event_type", "user_lifecycle", "user_id", 123)logger.Info("payment processed", "event_type", "payment", "amount", 99.99)Attributes and groups
Section titled “Attributes and groups”Use WithAttrs to add attributes to all logs, and WithGroup to nest
attributes:
handler := hbslog.New(client). WithAttrs([]slog.Attr{slog.String("service", "api")}). WithGroup("http")
logger := slog.New(handler)logger.Info("request handled", "status", 200, "method", "POST")This produces:
{ "event_type": "log", "level": "INFO", "message": "request handled", "service": "api", "http": { "status": 200, "method": "POST" }}Log level filtering
Section titled “Log level filtering”Control which logs are sent to Honeybadger:
// Only send WARN and abovehandler := hbslog.New(client).WithLevel(slog.LevelWarn)logger := slog.New(handler)
logger.Info("This is ignored")logger.Warn("This is sent")Use slog.LevelVar for dynamic level changes at runtime:
levelVar := new(slog.LevelVar)levelVar.Set(slog.LevelInfo)
handler := hbslog.New(client).WithLevel(levelVar)logger := slog.New(handler)
levelVar.Set(slog.LevelDebug) // Now debug logs will be sentzerolog
Section titled “zerolog”The zerolog adapter sends logs from the rs/zerolog package to Honeybadger
Insights.
Quick start
Section titled “Quick start”import ( "github.com/rs/zerolog" "github.com/honeybadger-io/honeybadger-go" hbzerolog "github.com/honeybadger-io/honeybadger-go/zerolog")
func main() { client := honeybadger.New(honeybadger.Configuration{ APIKey: "PROJECT_API_KEY", }) writer := hbzerolog.New(client) logger := zerolog.New(writer).With().Timestamp().Logger() logger.Info().Msg("hello")}Options
Section titled “Options”WithEventType
Section titled “WithEventType”Sets the default event type for all logs (default: "log"). Override per-log
by including an event_type field:
writer := hbzerolog.New(client, hbzerolog.WithEventType("app_log"))WithKeys
Section titled “WithKeys”Customize field names if your zerolog uses non-standard keys. The writer remaps
the time field to ts for Honeybadger:
writer := hbzerolog.New( client, hbzerolog.WithEventType("app_log"), hbzerolog.WithKeys("timestamp", "severity"), // defaults: "time", "level")