You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
importloggingimportlogfmterlogger=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.
@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
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
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
The text was updated successfully, but these errors were encountered: