Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Expand logging filter API to allow returning a LogRecord #92592

Closed
Assignees
vsajip
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
@adriangb

Description

@adriangb

Feature or enhancement

The current API for filters is(record: LogRecord) -> bool (really truthy/falsy return not necessarily abool).
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:

defreplace_message(record:logging.LogRecord):returnlogging.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 aLogRecord, so it ends up being a bit verbose to do so.
  2. This doesn't really help or apply to filters installed onlogging.Logger since those are run only for the logger that was called (and hence modifying modifying theLogRecord in place is no different from returning a new instance in most real world scenarios).

Metadata

Metadata

Assignees

Labels

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

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp