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

Already on GitHub? Sign in to your account

Do not configure the root logger. #741

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
2 participants
Contributor

schlamar commented Apr 18, 2013

Calling basicConfig leaks to other custom defined loggers, so this leads to undesirable behavior. For example in this code example log messages gets printed out twice:

logger = logging.getLogger('mylogger')
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.INFO)

loop = ioloop.IOLoop.current()
loop._stopped = True
loop.start()
logger.info('Test')

This PR changes the check of handlers by adding only the basicConfig handlers to the Tornado loggers.

Owner

bdarnell commented Apr 18, 2013

The root logger is implicitly configured by functions like logging.warning; you shouldn't rely on the root logger remaining unconfigured. Instead, if you attach a handler to a logger that would be redundant with a root handler, set the propagate attribute of that logger to false: http://docs.python.org/2/library/logging.html#logging.Logger.propagate

Contributor

schlamar commented Apr 18, 2013

Ah, thanks for that :)

However, I would still discourage usage of logging.basicConfig because it could be called from within a thread and you shouldn't do that...

Owner

bdarnell commented Apr 18, 2013

Yes, but given the existence of logging.warning and friends, all multithreaded apps should explicitly configure their loggers before starting any threads unless they're sure that these functions are never used.

Contributor

schlamar commented Apr 18, 2013

Agreed. Thanks again for the propagate hint :)

@schlamar schlamar closed this Apr 18, 2013

@schlamar schlamar deleted the unknown repository branch Apr 18, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment