possible bug in slog.Value json marshaling

317 views
Skip to first unread message

Steven Pelley

unread,
Sep 7, 2023, 2:30:09 PM9/7/23
to golang-nuts
Hi everyone,

Possibly a bug or potential improvement in slog.
This will sound excessively complicated, which I recognize and have changed my approach.  Regardless, I see an opportunity to improve surprising behavior.
I'm looking for feedback on whether I missed something in the slog documentation or if this is a valid bug or improvement, in which case I can file it.

slog.Value always json.marshals to an empty document, "{}", which to me was surprising behavior when using JsonHandler.  If a slog.Value is embedded in a logged non-slog.Value, for example a slice, it will effectively omit data.  On the other hand any tree of Values and Attrs passed directly to the log functions, without an intervening non-slog.Value object, marshals and logs properly.  I expect a slice, or any other non-slog.Value type, containing an slog.Value to marshal to json with the slog.Value's represented value.

Alternatively, the slog documentation (or specifically JsonHandler) could state that no non-slog.Value object passed to a log method may contain an slog.Value, and that LogValuers must maintain this invariant.

go1.21.0 darwin/arm64

I discovered this when logging (using JsonHandler) an object containing a list of objects whose logged data I wanted to redact/reduce.  I do this as a LogValuer whose LogValue function constructs and returns a new slog.GroupValue with a []slog.Value containing the redacted objects for logging.  Each Value is created by in turn calling Value.Resolve(Value.anyValue(...)) on the component items.  The resulting log contains a list of empty documents.  I think this is a reasonable use of LogValue, though I recognize that many people will suggest not logging slices.

Thanks,
Steve


Jonathan Amsterdam

unread,
Sep 11, 2023, 1:45:55 PM9/11/23
to golang-nuts
Hi Steve,
I'm not sure what the right answer is. Can you file this as a bug on go.dev/issues?

Steven Pelley

unread,
Sep 18, 2023, 10:17:40 AM9/18/23
to golang-nuts
filed https://github.com/golang/go/issues/62699 and expect any further discussion will happen there.
Reply all
Reply to author
Forward
0 new messages