Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging documentation for library cleanup #61277

Closed
ajs mannequin opened this issue Jan 29, 2013 · 3 comments
Closed

logging documentation for library cleanup #61277

ajs mannequin opened this issue Jan 29, 2013 · 3 comments
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@ajs
Copy link
Mannequin

ajs mannequin commented Jan 29, 2013

BPO 17075
Nosy @vsajip, @bitdancer, @cjerdonek

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2013-01-29.19:33:57.143>
created_at = <Date 2013-01-29.18:05:06.226>
labels = ['type-bug', 'invalid', 'docs']
title = 'logging documentation for library cleanup'
updated_at = <Date 2013-01-29.19:33:57.131>
user = 'https://bugs.python.org/ajs'

bugs.python.org fields:

activity = <Date 2013-01-29.19:33:57.131>
actor = 'r.david.murray'
assignee = 'docs@python'
closed = True
closed_date = <Date 2013-01-29.19:33:57.143>
closer = 'r.david.murray'
components = ['Documentation']
creation = <Date 2013-01-29.18:05:06.226>
creator = 'ajs'
dependencies = []
files = []
hgrepos = []
issue_num = 17075
keywords = []
message_count = 3.0
messages = ['180919', '180927', '180940']
nosy_count = 5.0
nosy_names = ['vinay.sajip', 'r.david.murray', 'ajs', 'chris.jerdonek', 'docs@python']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue17075'
versions = ['Python 2.7']

@ajs
Copy link
Mannequin Author

ajs mannequin commented Jan 29, 2013

This documentation states that libraries can turn off logging by adding a NullHandler:

http://docs.python.org/2/howto/logging.html#configuring-logging-for-a-library

This is not entirely true. It only holds true if the application which calls the library has not called basicConfig on the root logger. If it has, you get logs to both the root logger and the NullLogger, defeating the point of having added a NullLogger in the first place.

The correct way for a library to silence log messages is to both set a NullHandler and set propagate to false.

For an example of the behavior on the current docs, see:

  import logging
  import sys
  # Application configures its root logger
  logging.basicConfig(level=logging.DEBUG)
  
  # Library configures a NullLogger:
  logger = logging.getLogger('foo')
  logger.setLevel(logging.DEBUG)
  handler = logging.NullHandler()
  handler.setLevel(logging.DEBUG)
  logger.addHandler(handler)
  
  # Library then logs:
  logger.warning("BLAH")

This example is not terribly interesting, but the more interesting example is when the library configures a real log handler (e.g. in order to create a separate security log in an authorization module). In this case, the log messages will be sent to both the file log and the root logger by default, as long as any part of the application has configured the root logger.

IMHO, propagate should always be False for all new loggers, but at the very least the fact that it is True should be documented in the section on library logging...

@ajs ajs mannequin assigned docspython Jan 29, 2013
@ajs ajs mannequin added docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error labels Jan 29, 2013
@cjerdonek
Copy link
Member

This documentation states that libraries can turn off logging by adding a NullHandler:

I don't think that's what the documentation says. It says, "If for some reason you don’t want these messages printed *in the absence of any logging configuration* [my emphasis].... If the library user configures logging for application use, presumably that configuration will add some handlers, and if levels are suitably configured then logging calls made in library code will send output to those handlers, as normal."

In other words, the documentation is acknowledging that logging won't get turned off if something else configures it. This is the same as what you say further on:

It only holds true if the application which calls the library has not called basicConfig on the root logger.

If there's some other part of the documentation that says otherwise, can you include a direct quote in the comment so we know what words you are referencing?

The correct way for a library to silence log messages is to both set a NullHandler and set propagate to false.

This doesn't sound like good practice to me and isn't what the current docs were trying to say.

@bitdancer
Copy link
Member

Indeed. The whole point of that section is to explain how the library can refrain from spewing unwanted logging *if the application doesn't care about logging*. If the application does care (has configured logging), it would be wrong to block the logging.

I believe there is now a NullHandler or something similar set up by default, so I don't think this issue arises in Python3.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants