slog default logger: replace and then revert

158 views
Skip to first unread message

Chris Burkert

unread,
Apr 17, 2025, 6:04:46 PMApr 17
to golang-nuts
Dear all,

I thought I can temporarily replace the slog default logger and eventually revert to the previous one. But this seems not to be possible: https://play.golang.com/p/OQbgGm_DRLY

Can someone explain, what is happening and how I can achieve my goal?

Chris

Andrew Harris

unread,
Apr 18, 2025, 2:21:17 AMApr 18
to golang-nuts
There's some interaction between setup of `log` and `slog` that is not revertible.

Before `slog.SetDefault`, it's sort of the case that the `log` package's default resource is the destination for all global/default/standard logging (from `slog` or `log`). The mechanism is a little aggressive, even, in the details - there is a slog handler that outputs via a resource managed by the log function. The function is made visible via a pointer held in a `log/internal` package log and slog both import.

After `slog.SetDefault`, the opposite configuration occurs in the sense that `log` sends its output into `slog`. I don't believe this flip is revertible.

If you must have `log` formatting, I think the thing that might be possible to implement a `slog` handler with formatting that precisely matched the `log`/pre-`SetDefault` arrangement. Dunno if that implementation exists already somewhere. An easier bar would be to set a `slog.NewTextHandler` as default. Hope this helps.


Andrew Harris

unread,
Apr 18, 2025, 2:28:36 AMApr 18
to golang-nuts
Sorry for spam, quick clarification: where I said "there is a slog handler that outputs via a resource managed by the log function" that's a bit jumbled, I intended to say "resource managed by the log package".

Chris Burkert

unread,
Apr 22, 2025, 6:33:19 PMApr 22
to Andrew Harris, golang-nuts
Thanks Andrew,
I see. As you mentioned I get a consistent behaviour if I define my own logger in the first place.
Chris

--
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 [email protected].
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/a20ceaa1-1280-425f-b1f9-72552d3497a7n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages