log/slog ships with two default handlers: the TextHandler and the JSONHandler.
Overview
…
For more control over the output format, create a logger with a different handler. This statement uses New to create a new logger with a TextHandler that writes structured records in text form to standard error:
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))TextHandler output is a sequence of key=value pairs, easily and unambiguously parsed by machine. This statement:
logger.Info("hello", "count", 3)produces this output:
time=2022-11-08T15:28:26.000-05:00 level=INFO msg=hello count=3The package also provides JSONHandler, whose output is line-delimited JSON:
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) logger.Info("hello", "count", 3)produces this output:
{"time":"2022-11-08T15:28:26.000000000-05:00","level":"INFO","msg":"hello","count":3}
(There’s actually a third, the MultiHandler, that fans out logs to multiple handlers). You can also use a custom handler you’ve built, or one of many third-party library handlers out there in the wild…