Description
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.