[issue17749] root logging functions break logger configuration
New submission from Fabian: If I get and configure a named logger in the main module and another module that is imported uses the root logging functions (e.g., logging.info(Hello World!)) somehow affects the configuration of the first (named) logger. The file m1.py defines the logger testlogger and then imports the module m2.py. In m2.py the root logger is used through logging.info(...). This message apparantly doesn't appear anywhere. After the import in m1.py the previously defined logger is used through logger.info(...). This message appears (formatted) in the logfile: 2013-04-16 11:23:56,231 INFO Hello from __main__ and (unformatted) at stdout: INFO:testlogger:Hello from __main__ I did not expect this behavior, therefore I reported this bug. In my expectation the call to logging.info() should not affect the behavior of the named and configured logger. Main module (m1.py): import logging def setup_logging(): # made this a function to avoid cluttering the global namespace logger = logging.getLogger(testlogger) logger.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') handler = (logging.FileHandler(testlog.log), logging.StreamHandler()) handler = handler[0] # 0 : write to logfile, 1 : write to stdout handler.setFormatter(formatter) logger.addHandler(handler) return logger logger = setup_logging() import m2 logger.info(Hello from %s, __name__) Other module (m2.py) import logging logging.info(Hello from %s, __name__) I observed this behavior with Python v2.7.3 -- components: IO messages: 187061 nosy: fmueller priority: normal severity: normal status: open title: root logging functions break logger configuration versions: Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17749 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17749] root logging functions break logger configuration
Changes by Antoine Pitrou pit...@free.fr: -- nosy: +vinay.sajip ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17749 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17749] root logging functions break logger configuration
Vinay Sajip added the comment: No, this behaviour is as expected. The sequence of events: 1. You call setup_logging(), which creates a logger with level DEBUG, and add a file handler to it. 2. You import m2, which calls logging.info(), which adds a handler to the root logger (via basicConfig() - see the documentation note just below the documentation for the logging.log function). However, since the root logger's default level is WARNING, the logging.info() call produces no output. 3. You call logger.info(), which calls your file handler as expected, then calls the handlers of ancestor loggers (as documented - see http://docs.python.org/2.7/howto/logging.html#logging-flow for more info). This results in the message being output to file (via the handler you added in setup_logging) and to console (via the handler you added to the root logger in logging.info() in m2.py). Note that if you add a line logger.propagate = False in setup_logging before you return the logger, then the root logger's handler isn't called. However, the general best practice is to let propagation do the work (i.e. don't set it to False unless you have very specific needs). -- resolution: - invalid status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17749 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com