Repository navigation
Conversation
Add NewJSONHandler that writes log records like NewTextHandler up to the message, followed by the attributes as pretty JSON with tab indentation. Keys and JSON tokens are dimmed, values are plain, and tint.Attr colors apply like in the text handler. Attribute values are encoded like slog.JSONHandler, verified by TestSlogParity against both slog handlers. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
NewJSONHandler(w io.Writer, opts *Options) slog.Handlerthat writes tinted logs with attributes formatted as pretty JSON.Following
log/slog's design, the existinghandlerstruct now serves both formats via a newjson boolfield; the text path is unchanged.Output format
Everything up to the message is rendered exactly like
NewTextHandler. The attributes follow as a pretty JSON object with tab indentation, starting on a new line below the message. If a record has no attributes, no JSON object is rendered.{,},,) are dimmed; values are plain.tint.Attr/tint.Errcolors apply like in the text handler: dimmed colored key, colored value.slog.JSONHandler(errors as strings, durations as nanoseconds, times as RFC3339Nano, everything else viaencoding/jsonwithout HTML escaping).WithAttrsattributes are preformatted and cached, tracking open groups likeslog.commonHandler; empty groups are elided.Tests
TestJSONHandlerruns allhandlerTestscases against a newWantJSONexpectation.TestSlogParity(unifies the formerTestHandler_Consistency) verifies value-encoding parity byte-for-byte againstslog.TextHandlerandslog.JSONHandlerintext/jsonsubtests.🤖 Generated with Claude Code