Sorry for my wrong answer - I thought that should work.

https://go.dev//issues/61892 explains it: the default slog handler uses the 
default log.Logger.
This answers my other question, too: why is the default slog.Handler 
(newDefaultHandler) unexported? Because it's error prone.

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

works as the workaround: log.SetOutput (after slog.SetDefault) breaks the 
cycle (but you have to set the default flags, too).

Mike Schinkel a következőt írta (2023. augusztus 29., kedd, 3:00:01 UTC+2):

> Hi Tamás,
>
> Have you actually tried that and gotten it to work? It does not compile
> for me but this does (note method call vs. property reference):
>
> slog.SetDefault(slog.New(myHandler{Handler:slog.Default().Handler()}))
>
> However, when delegating the Handle() method it seems to cause an infinite
> loop:
>
> func (m MyHandler) Handle(ctx context.Context, r slog.Record) error {
>     return m.Handler.Handle(ctx, r)
> }
>
> See https://goplay.tools/snippet/qw07m0YflLd
>
> I know about this because just this past weekend I was trying to write a
> TeeHandler to output the default to the screen and JSON to a file just this
> past weekend and ran into an infinite loop problem with the default 
> handler.
>
> I tried my best to figure out why it needed to be structured the way it was
> in that it seems to call itself recursively. I wanted to post a question to
> this list to see if there was a workaround, or if not to see if there might
> be interest in allowing it to work, but I could not get my head around it 
> so
> eventually gave up and just used the TextHandler instead.
>
> Shame though. It would be nice to be able to reuse the default handler but
> AFACT it is not possible (though if I am wrong I would love for someone to
> show me how to get it to work.)
>
> -Mike
>
>
> On Monday, August 28, 2023 at 12:50:50 PM UTC-4 Tamás Gulácsi wrote:
>
> slog.SetDefault(slog.New(myHandler{Handler:slog.Default().Handler}))
>
> vl...@mailbox.org a következőt írta (2023. augusztus 28., hétfő, 15:06:37 
> UTC+2):
>
> Hi, 
>
> When reading trough the log/slog documentation, it seems one can create 
> a logger with a different handler, which is either NewTextHandler or 
> NewJSONHandler. 
>
> Why can't I configure the defaultHandler? Let's say I want my logger to 
> behave exactly like the defaultHandler, but output to a logfile or 
> Stdout instead. 
>
> The defaultHandler's output is different compared to the NewTextHandler: 
>
> slog.Info("ok"), gives me: 
>
> INFO ok 
>
> The NextTextHandler gives me: 
>
> level=INFO msg="ok" 
>
>
> Regards, 
>
>

-- 
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/97a5ffed-670b-418f-90f2-4532b7831a64n%40googlegroups.com.

Reply via email to