Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: include extra fields in formatter #11

Open
matteo-zanoni opened this issue Jan 23, 2024 · 2 comments · May be fixed by #12
Open

Enhancement: include extra fields in formatter #11

matteo-zanoni opened this issue Jan 23, 2024 · 2 comments · May be fixed by #12

Comments

@matteo-zanoni
Copy link

matteo-zanoni commented Jan 23, 2024

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

@jteppinette
Copy link
Owner

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

@matteo-zanoni
Copy link
Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants