Open
Description
Zerolog has this nifty api log.Err(err error)
that is Error
level if err != nil
and Info
level if err == nil
The basic gist to avoid this common code:
authenticated, err := auth()
if err != nil {
slog.Error(ctx, "failed to authorize user"
slog.Error(err),
slog.F("user", "alice")
)
return
}
slog.Info(ctx, "authorized user",
slog.F("user", "alice"),
)
...
and you can just do (the message becomes the action, and the Error level indicates the action failed):
authenticated, err := auth()
// Unsure exactly on the API, still adapting to slog, so unsure what would be "natural"
slog.Err(err).(ctx, "authorize user",
slog.F("user", "alice")
)
if err != nil {
return
}
...
It is a small feature I miss. Let me know what you think.
Metadata
Metadata
Assignees
Labels
No labels