coder / coder/slog

Implement api to accept empty error

Open
#94 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
351
Forks
23
PR merge metrics
No merged PRs in 30d

Description

Zerolog has this nifty api [`log.Err(err error)`](https://github.com/rs/zerolog/blob/master/log.go#L320-L330) that is `Error` level if `err != nil` and `Info` level if `err == nil`

The basic gist to avoid this common code:
```go
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):

```go
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.