Module slogx provides extensions and helpers for the log/slog package.
Package slogx provides Logger as an alternative to slog.Logger, which focuses on performance and makes several design changes to improve it:
- It does not provide convenience methods for attributes that may affect performance. All methods accept attributes only as slog.Attr.
- It provides an option to disable the inclusion of source information in the log Record. This can improve performance by up to 100% in cases where the source information is not included by the Handler anyway.
- Its With method does not immediately call the handler's WithAttrs method, instead it buffers up to 4 attributes which are then added to each log Record. This improves performance when you need to define a temporary set of attributes in a function and log a few messages with those attributes a few times. It also greatly improves performance when the logger is disabled. This is because calling WithAttrs is quite an expensive operation, especially if the Handler is wrapped multiple times. That is, each layer will call the underlying handler's WithAttrs method and cause a lot of allocations. But what if the message is discarded because the logger is disabled? Yes, it will be a waste of CPU time. So for temporary With attribute sets, it is usually more efficient to keep them on the Logger. If any of the WithGroup, Handler, or LongTerm methods are called later, the temporary attributes will be flushed using the WithAttrs method.
- It provides the WithLongTerm method, which acts as a sequence of With and LongTerm method calls and is needed for cases where the resulting logger is intended to be reused multiple times and may reside in a rather long-lived context.
- See benchmark results for details.