GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Expand logging filter API to allow returning a LogRecord · Issue #92592 · python/cpython

Expand logging filter API to allow returning a LogRecord #92592

New issue
Copy link
New issue
Copy link
Closed
#92591
Closed
Expand logging filter API to allow returning a LogRecord#92592
#92591
Copy link
Assignees
vsajip
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@adriangb
adriangb
opened on May 10, 2022
Issue body actions

Feature or enhancement

The current API for filters is (record: LogRecord) -> bool (really truthy/falsy return not necessarily a bool).
I would like to propose that we change the API to be (record: LogRecord) -> bool | LogRecord.

Pitch

Currently logging filters are the only way to hook into the logging system to enrich or otherwise modify log records, but they are limited by the fact that you have to modify a log record in place, thus propagating this change in everywhere, even if you only want to apply this change to a specific handler or logger.

If the filterer returns a log record, it is indicating that that the reference to the log record passed in as an argument should be replaced with this log record (which may be the same instance or a completely new instance) and that logging should continue.

An example filter might look like:

def replace_message(record: logging.LogRecord):
    return logging.LogRecord(
        name=record.name,
        level=record.levelno,
        pathname=record.pathname,
        lineno=record.lineno,
        msg="new message!",
        exc_info=record.exc_info,
        args=(),
    )

You could then apply this filter to only 1 out of 2 handlers, which would result in one handler always logging "new message!" and the other being unaffected by the change.

Previous discussion

https://discuss.python.org/t/expand-logging-filter-api-to-allow-returning-a-logrecord/15621

Cons of this proposal

  1. There is no good API for creating or copying a LogRecord, so it ends up being a bit verbose to do so.
  2. This doesn't really help or apply to filters installed on logging.Logger since those are run only for the logger that was called (and hence modifying modifying the LogRecord in place is no different from returning a new instance in most real world scenarios).

Activity

  1. adriangb
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on May 10, 2022
  2. serhiy-storchaka
    assigned
    vsajip
    on May 10, 2022
  3. erlend-aasland
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on May 16, 2022
  4. serhiy-storchaka
    added this to Logging issues 🪵on May 20, 2022
  5. vsajip
    closed this as completedin #92591on Jun 7, 2022
  6. vsajip
    moved this to Done in Logging issues 🪵on Jun 7, 2022
  7. vsajip
    added a commit that references this issue on Jun 7, 2022

    gh-92592: Allow logging filters to return a LogRecord. (GH-92591)

    296081a
  8. serhiy-storchaka
    added a commit that references this issue on Jun 26, 2022

    Revert 30935 defer bpo45162 to 312 (#94285)

    1ffde3e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @vsajip
    vsajip

Labels

stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Projects

  • Logging issues 🪵
    Status
    Done
    Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.