Apex log to Structured log

github.com/apex/log versus log/slog

Published: Wednesday, Aug 9, 2023 Last modified: Thursday, Nov 14, 2024

https://github.com/apex/log versus go1.21’s log/slog

log.WithField("record", rec).Info("adding")
slog.Info("test", "record", rec)

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

log.WithError(errors.New("unauthorized")).Error("upload failed")
slog.Error("upload failed", "err", errors.New("unauthorized"))

Not sure how to do a Fatal

log.WithError(err).Fatal("error listening")

Setting the default handler to JSON output:

log.SetHandler(jsonhandler.Default)
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))

Default handler outputting to stderr.

log.WithFields(log.Fields{
        "url":     logoutUrl,
        "profile": profile,
        }).Info("logouting user out")