Log handler for the Go log/slog package
There are three handlers implemented in this package with a heavy focus on the development handler.
- Development - logging in development mode.
- Discard - all gets discarded, useful for integration testing where log lines can become noise.
- Production - a wrapper around slog's JSON handler with sensible defaults.
This handler should only be used in development. It formats the log lines where the attribute keys are color coded.
It is encouraged to always set the default logger with slog.SetDefault(...)
in order to not "depend" directly on this logger in your code base.
devLogger := golog.NewDevelopment()
slog.SetDefault(devLogger)
slog.Info("this log will be pretty printed")
Example code used to generate the output: https://go.dev/play/p/Y0d-i5_SutP
If the data passed in as a string
or []byte
is a valid JSON object, it will be automatically formatted, colorized, and its type marked as JSON.
All log lines will be discarded. Should be used for integration tests.
discardLogger := golog.NewDiscard()
slog.SetDefault(discardLogger)
slog.Info("this log will be discarded")
The production handler is just a wrapper for slog's JSON handler with a few sensible defaults:
- Logs are output to
stderr
to align it to the POSIX standard - The default log level is
slog.LevelInfo
- slog's TimeKey is replaced in JSON to a property named
t
- slog's source is enabled and formatted to
<file:line>
, e.g.my_file.go:17
prodLogger := golog.NewProduction()
slog.SetDefault(prodLogger)
slog.Info("this log will be output to stderr and formatted as JSON")