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

Added defaults parameter for logging.Formatter #85061

Closed
bharel mannequin opened this issue Jun 5, 2020 · 5 comments
Closed

Added defaults parameter for logging.Formatter #85061

bharel mannequin opened this issue Jun 5, 2020 · 5 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@bharel
Copy link
Mannequin

bharel mannequin commented Jun 5, 2020

BPO 40884
Nosy @vsajip, @bharel, @miss-islington, @remilapeyre
PRs
  • bpo-40884: Added defaults parameter for logging.Formatter #20668
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-06-29.17:09:05.435>
    created_at = <Date 2020-06-05.23:29:51.873>
    labels = ['type-feature', 'library', '3.10']
    title = 'Added defaults parameter for logging.Formatter'
    updated_at = <Date 2020-06-29.17:09:05.434>
    user = 'https://github.com/bharel'

    bugs.python.org fields:

    activity = <Date 2020-06-29.17:09:05.434>
    actor = 'vinay.sajip'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-06-29.17:09:05.435>
    closer = 'vinay.sajip'
    components = ['Library (Lib)']
    creation = <Date 2020-06-05.23:29:51.873>
    creator = 'bar.harel'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40884
    keywords = ['patch']
    message_count = 3.0
    messages = ['370796', '370802', '371816']
    nosy_count = 4.0
    nosy_names = ['vinay.sajip', 'bar.harel', 'miss-islington', 'remi.lapeyre']
    pr_nums = ['20668']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40884'
    versions = ['Python 3.10']

    @bharel
    Copy link
    Mannequin Author

    bharel mannequin commented Jun 5, 2020

    TLDR; logging.Formatter('%(ip)s %(message)s', defaults={"ip": None})

    Python's logging.Formatter allows the placement of custom fields, e.g.
    logging.Formatter("%(ip)s %(message)").

    If a handler has a formatter with a custom field, all log records that go through the handler must have the custom field set using extra={}.
    Failure to do so will result in exceptions thrown inside the logging library.

    Custom fields are common, and are even suggested by the Python logging cookbook, where they are attached to the root logger.

    There is, however, no way to specify default values for the custom fields. Quite a few issues arise from it.

    For example, if I've set a formatter on the root logger with the custom field "%(ip)s", all logging messages sent by the asyncio library, will cause exceptions to raise.

    Adding default values is possible using LoggerAdapter but will causes other issues as well as not solve the aforementioned problem.

    Adding default values is possible using Filters, but cause confusion, isn't simple, and permanently modify the record object itself, which can cause issues if more handlers or formatters are attached.

    From a quick search, this feature was asked for many times in stackoverflow, and even spawned up a few libraries such as "logaugment" in order to solve it.

    I believe the solution offered, by using defaults={} is simple enough to not need discussion over python-ideas, yet common enough to justify the addition to the standard library.

    I've provided a reference PR. It does not cause backwards compatibility issues, complies with all formatter styles (%, {}, $), passes all tests and is simple enough to both use and understand.

    Not sure if 3.9 is feature-closed for small additions like this.

    @bharel bharel mannequin added 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jun 5, 2020
    @remilapeyre
    Copy link
    Mannequin

    remilapeyre mannequin commented Jun 6, 2020

    New features will go in Python 3.10 indeed.

    @remilapeyre remilapeyre mannequin removed 3.9 only security fixes labels Jun 6, 2020
    @miss-islington
    Copy link
    Contributor

    New changeset 8f192d1 by Bar Harel in branch 'master':
    bpo-40884: Added defaults parameter for logging.Formatter (GH-20668)
    8f192d1

    @vsajip vsajip closed this as completed Jun 29, 2020
    @vsajip vsajip closed this as completed Jun 29, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @ayoisaiah
    Copy link

    It looks like defaults isn't supported in dictConfig() at the moment https://docs.python.org/3/library/logging.config.html#logging-config-dictschema

    @bharel
    Copy link
    Contributor

    bharel commented Apr 8, 2023

    @ayoisaiah fixing.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants