Enhancement: include extra fields in formatter
Currently, the formatter lacks the ability to include static extra fields in the log output. It is possible to include extra fields in each call (.info, .error...) but this is not always the most ergonomic way. This enhancement proposes adding support for including extra fields within the formatter configuration.
Proposed Solution
Introduce a new feature in the logging formatter that allows users to specify extra fields to be included in the log output. This could be achieved by extending the existing formatter configuration with an optional parameter in the form of a dictionary of key and value of the extra fields.
Example
import logging
import logfmter
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
formatter = logfmter.Logfmter(
keys=["level", "module", "custom_field1", "custom_field2"],
mapping={
"level": "levelname",
"module": "name",
},
default_extra: {
"custom_field1": "aaa",
"custom_field2": "bbb",
})
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("the fields in default_extra will be included")
Impact
This enhancement is backward-compatible and does not affect existing code that does not utilize the new feature. It provides an opt-in mechanism for users to take advantage of the enhanced formatter.
Related Issues/PRs
#10
@matteo-zanoni I added a guide section which talks about adding pairs via the log record factory. I am not totally convinced that's not the most pythonic way of solving this problem.
@jteppinette I totally agree with you, using the log factory in this manner seems hacky to me too. If you think this proposal has a more elegant API I would be happy to implement it and open a PR. Let me know
As commented on the suggested PR in https://github.com/jteppinette/python-logfmter/pull/12#issuecomment-2612197942 This can often be solved better by a filter or if they are dynamic a LoggerAdapter