Yesterday, I came up with the same question and found this: "github.com/lmittmann/tint"
(This solution still uses os.Stdout, but I think this can do what you need.) An example: logOptions := &tint.Options{ NoColor: true, Level: slog.LevelError, TimeFormat: time.DateTime, } logHandler := tint.NewHandler(os.Stdout, logOptions) logger := slog.New(logHandler) slog.SetDefault(logger) slog.Info("this does not show") slog.Debug("this debug info does not show") logOptions.Level = slog.LevelInfo slog.Info("this is now visible") slog.Debug("this debug info still does not show") logOptions.Level = slog.LevelDebug slog.Info("this is still visible") slog.Debug("this debug info also shows") Op dinsdag 29 augustus 2023 om 03:00:01 UTC+2 schreef Mike Schinkel: > 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/df136f1e-0283-46fa-a0b7-ce17a0722fd9n%40googlegroups.com.