Logy|Docs
Formatting

Error logging

Include errors and stack traces in log records.

When an error is passed as the last argument, Logy can include the error and stack trace in the output.

Pass the error separately when the message should describe the business event and the handler should format the error details.

main.go
err := errors.New("insert failed")

log.Error("user {} was not inserted", userID, err)

Do not add a {} placeholder for the error when you want the error trace to be printed separately.

Return and log at the boundary

Prefer returning errors from lower-level functions and logging them at the boundary where you have request, job, or operation context.

handler.go
func handleCreateUser(ctx context.Context, user User) {
	log := logy.Get()

	if err := service.CreateUser(ctx, user); err != nil {
		log.Error("create user {} failed", user.ID, err)
		return
	}

	log.I(ctx, "user {} created", user.ID)
}

This avoids duplicate logs while still keeping useful context on the failure.