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
{{ message }}
Repository navigation
Expand logging filter API to allow returning a LogRecord #92592
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.
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.
There is no good API for creating or copying a LogRecord, so it ends up being a bit verbose to do so.
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
gh-92592: Allow logging filters to return a LogRecord. (GH-92591)
Revert 30935 defer bpo45162 to 312 (#94285)