This solution is built on top of Go's log/slog
package and leverages slog.Default()
. To use it a developer should declare a package variable logger
in every package that is part of their Go project that needs to perform logging (e.g. a package containing only type
, var
and/or const
declarations would usually not need a logger.go
file.)
Add a logger.go
file to every packages and then add this line to the file, updating it with the package's name:
var logger = logging.NewPackageLogger("<package_name>")
This uses a small logging
package you can find below which has a Logger
type simply to embed slog.Log
. This allows adding extra methods beyond that provided by slog.Logger
and to preprocess calls to *slog.Logger
if we later find that to be needed.