Skip to content

Implement api to accept empty error #94

@Emyrk

Description

@Emyrk

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions