Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pr feedback
  • Loading branch information
adriangb committed May 10, 2022
commit 225329fcb4d192a07490177a078e753e2f32ac14
19 changes: 6 additions & 13 deletions Doc/howto/logging-cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -713,28 +713,21 @@ which, when run, produces something like:
2010-09-06 22:38:15,301 d.e.f DEBUG IP: 123.231.231.123 User: fred A message at DEBUG level with 2 parameters
2010-09-06 22:38:15,301 d.e.f INFO IP: 123.231.231.123 User: fred A message at INFO level with 2 parameters

Imparting contextual in Handlers
---------------------------------
Imparting contextual information in handlers
--------------------------------------------

Each :class:`~Handler` has it's own chain of Filters.
Each :class:`~Handler` has its own chain of filters.
If you want to add contextual information to a :class:`LogRecord` without leaking
this contextual information to other handlers, you can use a filter that returns
it to other handlers, you can use a filter that returns
a new :class:`~LogRecord` instead of modifying it in-place:

script::

import copy
import logging

def filter(record: logging.LogRecord):
Comment thread
vsajip marked this conversation as resolved.
record = logging.LogRecord(
name=record.name,
level=record.levelno,
pathname=record.pathname,
lineno=record.lineno,
msg=record.msg,
exc_info=record.exc_info,
args=(),
)
record = copy.copy(record)
record.user = 'jim'
return record

Expand Down
6 changes: 3 additions & 3 deletions Doc/library/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,8 @@ empty string, all events are passed.

.. method:: filter(record)

Is the specified record to be logged? Returns zero for no, nonzero for
yes. Filters can also modify log records in-place or return a completely
Is the specified record to be logged? Returns falsy for no, truthy for
yes. Filters can either modify log records in-place or return a completely
different record instance which will replace the original
log record without modifying it.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the without modifying it should be replaced by in any future processing of the event.


Expand All @@ -689,7 +689,7 @@ which has a ``filter`` method with the same semantics.

.. versionchanged:: 3.12
You can now return a :class:`LogRecord` instance from filters to replace
the log record without modifying it in place. This allows filters installed
the log record without modifying it in place. This allows filters attached to
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without -> , rather than

on a :class:`Handler` to modify the log record before it is emitted without
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attached to a handler, not attached to on a handler.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, corrected

having side effects on other handlers.

Expand Down
14 changes: 8 additions & 6 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,15 +817,17 @@ def filter(self, record):
Determine if a record is loggable by consulting all the filters.

The default is to allow the record to be logged; any filter can veto
this by returning a falsy value and the record is then dropped and
this method returns a falsy value.
Filters can return a log record, which case that log record
is used to call the next filter.
If filters return a truthy value that is not a log record the
next filter is called with the existing log record.
this by returning a falsy value.
If a filter attached to a handler returns a log record instance,
then that instance is used in place of the original log record in
any further processing of the event by that handler.
If a filter returns any other truthy value, the original log record
is used in any further processing of the event by that handler.

If none of the filters return falsy values, this method returns
a log record.
If any of the filters return a falsy value, this method returns
a falsy value.

.. versionchanged:: 3.2

Expand Down
22 changes: 4 additions & 18 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,9 @@ def log_at_all_levels(self, logger):

def test_handler_filter_replaces_record(self):
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=(),
)
record = copy.copy(record)
record.msg = "new message!"
return record

# Set up a logging hierarchy such that "child" and it's handler
# (and thus `replace_message()`) always get called before
Expand Down Expand Up @@ -507,15 +501,7 @@ def test_logging_filter_replaces_record(self):
class RecordingFilter(logging.Filter):
def filter(self, record: logging.LogRecord):
records.add(id(record))
return logging.LogRecord(
name=record.name,
level=record.levelno,
pathname=record.pathname,
lineno=record.lineno,
msg=record.msg,
exc_info=record.exc_info,
args=(),
)
return copy.copy(record)

logger = logging.getLogger("logger")
logger.setLevel(logging.INFO)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Let :mod:`logging` Filters to return a :class:`logging.LogRecord` instance
so that :class:`logging.Handler`\ s can enrich records without side effects on
other Handlers.
Let :mod:`logging` filters to return a :class:`logging.LogRecord` instance
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still says Let ... to which is incorrect. Did you forget to make this change, which I suggested earlier?

Copy link
Copy Markdown
Contributor Author

@adriangb adriangb May 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it looks like I missed that part of the comment, thank you for bringing it back up and for your patience

so that filters attached to :class:`logging.Handler`\ s can enrich records without
side effects on other handlers.