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
edit docs
  • Loading branch information
adriangb committed May 10, 2022
commit f1ea183f6f98871687084440ede535a628681be8
35 changes: 35 additions & 0 deletions Doc/howto/logging-cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,41 @@ 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 information for specific Handlers
------------------------------------------------------

Each :class:`~Handler` has it's own chain of Filters.
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.

it's should be its - the former is only used as a contraction of it is. Also, no need to capitalise filters here.

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
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.

You could shorten this contextual information to just it.

a new :class:`~LogRecord` instead of modifying it in-place:

script::

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.user = 'jim'
return record

if __name__ == '__main__':
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(message)s from %(user)-8s')
handler.setFormatter(formatter)
handler.addFilter(filter)
logger.addHandler(handler)

logger.info('A log message')

.. _multiple-processes:

Expand Down
11 changes: 9 additions & 2 deletions Doc/library/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,9 @@ empty string, all events are passed.
.. method:: filter(record)

Is the specified record to be logged? Returns zero for no, nonzero for
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.

After the change, this should be `a 'falsy' value for no, and a 'truthy' value for yes.

yes. If deemed appropriate, the record may be modified in-place by this
method.
yes. Filters can also modify log records in-place or return a completely
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.

also -> either

different log-record which will replace the original log record without
modifying it.

Note that filters attached to handlers are consulted before an event is
emitted by the handler, whereas filters attached to loggers are consulted
Expand All @@ -686,6 +687,12 @@ which has a ``filter`` method with the same semantics.
parameter. The returned value should conform to that returned by
:meth:`~Filter.filter`.

.. 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
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.

installed on -> attached to

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.

Although filters are used primarily to filter records based on more
sophisticated criteria than levels, they get to see every record which is
processed by the handler or logger they're attached to: this can be useful if
Expand Down