We’ve already discussed when the default log/slog Logger also serves as the default log Logger. But what if you want to send log logs to a log/slog Logger, without using the global defaults?
Introducing NewLogLogger!
func NewLogLogger
func NewLogLogger(h Handler, level Level) *log.LoggerNewLogLogger returns a new log.Logger such that each call to its Output method dispatches a Record to the specified handler. The logger acts as a bridge from the older log API to newer structured logging handlers.
Well that was easy!
Of course one may wonder why you would want this.
I know of two reasons:
- You depend on an old library that expects a
*log.Logger. - You’re still mid-migration from
logtolog/slog. Use this as a backward compatibility shim. (Really, a special case of #1)