Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: log/slog: add JSONHandler.SetIndent method #71127

Closed
icholy opened this issue Jan 4, 2025 · 3 comments
Closed

proposal: log/slog: add JSONHandler.SetIndent method #71127

icholy opened this issue Jan 4, 2025 · 3 comments
Labels
Milestone

Comments

@icholy
Copy link

icholy commented Jan 4, 2025

Proposal Details

I'd like to be able configure indentation when using the slog.JSONHandler in development. I'm aware that I can implement my own handler which support whatever formatting I like, but it would nice if this was built-in.

Proposal:

Add a JSONHandler.SetIndent method which mirrors the json.Encoder.SetIndent method.

package slog

// SetIndent instructs the handler to format the json output.
// Calling SetIndent("", "") disables indentation.
func (h *JSONHandler) SetIndent(prefix, indent string) 

Considerations:

I took at look the JSONHandler implementation and it appears to implement its own json encoder rather than using encoding/json. I assume this is for performance reasons. In light of this, adding indentation support probably isn't worth it. But I'll leave this here anyway.

@icholy icholy added the Proposal label Jan 4, 2025
@gopherbot gopherbot added this to the Proposal milestone Jan 4, 2025
@seankhliao
Copy link
Member

https://go.dev/play/p/CPttVkMOhrT

@adonovan
Copy link
Member

adonovan commented Jan 4, 2025

cc: @jba

@dmitshur dmitshur changed the title proposal: log/slog: Add JSONHandler.SetIndent method proposal: log/slog: add JSONHandler.SetIndent method Jan 4, 2025
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Jan 5, 2025
@jba
Copy link
Contributor

jba commented Jan 5, 2025

@seankhliao Thanks! I don't think I knew about json.Indent.

Closing, because Sean's solution is so simple.

@jba jba closed this as completed Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants