This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: logging documentation clarifications
Type: enhancement Stage: resolved
Components: Documentation Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: chris.jerdonek, docs@python, python-dev, vinay.sajip
Priority: normal Keywords: easy

Created on 2013-01-21 17:31 by chris.jerdonek, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg180344 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2013-01-21 17:31
Here are some suggestions of things to clarify in the logging documentation after consulting it as an end-user:

1. Clarify in Logger.filter(), Handler.filter(), and probably also in the Filter section that the case of more than filter behaves as follows: filters are applied until a filter says the record should not be processed.  In particular, a record gets processed only if all filters say it should be processed rather than at least one.

This is especially worth clarifying because with the above behavior combined with the Filter class's default behavior, it never makes sense to add more than one filter (because you can replace a group of filters with their lowest common ancestor).  It's only needed with custom filters.

2. Clarify that a handler can log the same record multiple times if it is attached to an ancestor logger.  This clarification perhaps best goes in the Logger.propagate section.  Currently, it's not clear whether loggers/handlers are "smart" in the sense that they keep track of whether a given message has already been passed to a given handler (independent of what loggers it is attached to): "Logger.propagate: If this evaluates to true, logging messages are passed by this logger and by its child loggers to the handlers of higher level (ancestor) loggers."

Incidentally, is there any case where you would want the same handler to process the same record more than once?

3. Clarify in the LogRecord section that the "name" attribute refers to the name of the logger used in the end-user's code rather than the name of the logger handling the message.  In particular, a record logged by a logger and an ancestor logger will have the same "name" field for both.

4. Clarify the last part of this sentence: "Note that filters attached to handlers are consulted whenever an event is emitted by the handler, whereas filters attached to loggers are consulted whenever an event is logged to the handler (using debug(), info(), etc.)"

It should say something like, "whereas filters attached to loggers are consulted prior to sending an event to its handlers."  In particular, there can be more than one handler, and it happens before rather than after being logged to the handlers ("whenever" is somewhat ambiguous).
msg180352 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-01-21 19:46
New changeset 871519e1f146 by Vinay Sajip in branch '2.7':
Issue #17007: Improved logging documentation based on suggestions in the issue.
http://hg.python.org/cpython/rev/871519e1f146

New changeset 029785354dbc by Vinay Sajip in branch '3.2':
Issue #17007: Improved logging documentation based on suggestions in the issue.
http://hg.python.org/cpython/rev/029785354dbc

New changeset 1902e86dbb88 by Vinay Sajip in branch '3.3':
Issue #17007: Merged doc update from 3.2.
http://hg.python.org/cpython/rev/1902e86dbb88

New changeset 6110ed94f86d by Vinay Sajip in branch 'default':
Closes #17007: Merged doc update from 3.3.
http://hg.python.org/cpython/rev/6110ed94f86d
msg180354 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2013-01-21 19:53
Your suggestions are good, and I implemented them more or less as you suggested. Additional comments:

> it never makes sense to add more than one filter

Except for readability. Although in theory one filter could do the work of several, it may be that different filters are added independently from configurations based on particular requirements.

> Clarify that a handler can log the same record multiple times

I've mentioned this as *emit* the same record multiple times.

> Incidentally, is there any case where you would want the same handler to process the same record more than once?

I can't think why you'd ever want this - it would just duplicate information in the log output.

> Clarify in the LogRecord section that the "name" attribute refers to the name of the logger used in the end-user's code

I think that part is clear enough by saying that it's the name of the logger used to log the event, but I've added a rider that this is so even when the event is emitted by a handler attached to an ancestor logger.

> Clarify the last part of this sentence: "Note that filters attached to handlers are consulted ..."

Done, this should now be clearer.
msg180359 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2013-01-21 20:42
Great, that looks a lot better.  Thanks!

A couple comments though:

+   .. note:: If you attach a handler to several loggers, it may emit the same
+      record multiple times. In general, you should not need to attach a
+      handler to more than one logger - if you just attach it to the
+      appropriate logger which is highest in the logger hierarchy, then it
+      will see all events logged by all descendant loggers, provided that
+      their propagate setting is left set to ``True``. A common scenario is to
+      attach handlers only to the root logger, and let propagation take care of
+      the rest.

Would it be better to say something like, "If you attach a handler to a logger and one of its ancestors [and similar changes later]..."  The reason I ask is that it seems like there is a valid use case for attaching a handler to more than one logger (which I have used), which is if the loggers aren't in the same ancestry chain.  For example, if I want loggers "app1" and "app2" to log and nothing else, I could attach a StreamHandler to those two loggers and a NullHandler to the root logger.  It seems like the above would discourage that pattern.  Or is that not recommended?  (I suppose you could also do this using a custom filter by name on the root logger, though I don't know which technique is preferred.)

> it never makes sense to add more than one filter

Here I was referring to the restricted case of using default filters by name.  For example, it seems like it would never make sense to filter by "A.B" and "A.C" because that is just an obfuscated way of filtering by the simpler and equivalent "A."  Does that make sense?  (I'm not suggesting any changes to the docs in this comment.)
msg180361 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-01-21 21:58
New changeset 8de6f92c89e6 by Vinay Sajip in branch '2.7':
Issue #17007: Made minor changes to documentation wording.
http://hg.python.org/cpython/rev/8de6f92c89e6

New changeset c8614ec54a63 by Vinay Sajip in branch '3.2':
Issue #17007: Made minor changes to documentation wording.
http://hg.python.org/cpython/rev/c8614ec54a63

New changeset d0e9f64bd55c by Vinay Sajip in branch '3.3':
Issue #17007: Merged minor changes from 3.2.
http://hg.python.org/cpython/rev/d0e9f64bd55c

New changeset 6877957a5e91 by Vinay Sajip in branch 'default':
Issue #17007: Merged minor changes from 3.3.
http://hg.python.org/cpython/rev/6877957a5e91
History
Date User Action Args
2022-04-11 14:57:40adminsetgithub: 61209
2013-01-21 21:58:44python-devsetmessages: + msg180361
2013-01-21 20:42:55chris.jerdoneksetmessages: + msg180359
2013-01-21 19:53:35vinay.sajipsetmessages: + msg180354
2013-01-21 19:46:32python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg180352

resolution: fixed
stage: resolved
2013-01-21 17:31:40chris.jerdonekcreate