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.
On Friday, December 15, 2023 at 2:19:22 AM UTC-5 Uli Kunitz wrote:

> 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.
> On Wednesday, December 13, 2023 at 3:30:53 AM UTC+1 Billy Lynch wrote:
>
>> 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 
>> <https://pkg.go.dev/log/slog#hdr-Wrapping_output_methods>. 
>>
>> Helpers have been suggested as a workaround for this 
>> <https://github.com/golang/go/issues/59145#issuecomment-1481920720>, 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 
>> <https://pkg.go.dev/log/slog#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!
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/3660a04f-d4d6-4ed5-b364-c9c49efab060n%40googlegroups.com.

Reply via email to