slog formatter funcs? (e.g. slog.Infof)

254 views
Skip to first unread message

Billy Lynch

unread,
Dec 12, 2023, 9:30:53 PM12/12/23
to golang-nuts
Hi!

I've been playing around with the new slog package, and I noticed that it was missing formatter funcs (Infof, Errorf, etc). I'm interested in adding these, but I wasn't sure if this was done intentionally since the godoc calls out this type of functionality in examples.

Helpers have been suggested as a workaround for this, but this is common enough behavior for me that I think it'd be very helpful to have upstream to make it easy to do things like: slog.Errorf("error calling func: %v", err)

Rough idea would be do expose formatter functions in the Logger, with the tradeoff that you replace formatter args for Attr args:

```go
func (l *Logger) Errorf(format string, args ...any) {
l.log(context.Background(), LevelError, fmt.Sprintf(msg, args...))
}

func (l *Logger) ErrorContextf(ctx context.Background(ctx context.Context, format string, args ...any) {
l.log(ctx, LevelError, fmt.Sprintf(msg, args...))
}
```

I wanted to check in before I opened an issue! 🙏
Let me know if this makes sense, or if there's context I'm missing.

Thanks!

Axel Wagner

unread,
Dec 13, 2023, 4:49:19 AM12/13/23
to Billy Lynch, golang-nuts
I believe the intention is that you shouldn't format this, but make `err` an `Attr`. i.e. the philosophy behind structured logging is specifically, not to do formatting, as it produces unstructured data. Whether I'm personally convinced by that philosophy is another question, but I think that's the reason these where left out.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9316489a-3a0c-4b8e-b789-534662a9bbben%40googlegroups.com.

Uli Kunitz

unread,
Dec 15, 2023, 2:19:22 AM12/15/23
to golang-nuts
The documentation of the package says this: "Package slog provides structured logging, in which log records include a message, a severity level, and various other attributes expressed as key-value pairs."


If you are using string formatting you are not producing key-value pairs. Following the structured logging philosophy your example should be slog.Error("func returned error", "err", err). I would include a second key-value pair "func", "<functionName>" too.


Such key value pairs are much easier to filter by log management and processing tools than formatted strings. It also helps to reduce the CPU consumption of the logging function itself. The slog package provides the LogAttrs function for that purpose.

Jonathan Amsterdam

unread,
Dec 17, 2023, 9:10:32 PM12/17/23
to golang-nuts
I personally am a fan of formatted logs, but we didn't see a way to combine them with key-value pairs (you only get one variadic arg per function call).
It wouldn't be hard to write a layer that wraps a slog.Logger or slog.Handler with xxxf functions. That's what I recommend.
Reply all
Reply to author
Forward
0 new messages