Search code examples
python-2.7loggingenthought

Enthought Canopy & IPython QtConsole ignore logging.basicConfig


Using Enthought Canopy v1.4 on Mac (Mavericks), logging statements that are debug or info are ignored, even with a logging level of DEBUG. The PyLab backend is set to Interactive (Qt4). Here is an example and its output:

import logging

logging.basicConfig(level=logging.DEBUG)

logging.debug('variable is nil')
logging.info('hey! listen!')
logging.warning('watch out!')
logging.error('an error happened')
logging.critical('a critical event happened')

Will log to Canopy’s “interactive data-analysis environment” built-in console:

WARNING:root:watch out!
ERROR:root:an error happened
CRITICAL:root:a critical event happened

Running this in Terminal will ignore debug/info and output right there. Interestingly setting the logging.basicConfig to use a log file and then running in Terminal will not ignore debug/info.

I can workaround this, but is there a solution?


Solution

  • Interesting. Canopy's IPython shell is an "IPython Qtconsole", and what you are seeing is the normal behavior of QtConsole. To see this, open a standard (non-Canopy) Qtconsole from a Terminal with ipython qtconsole

    Then in this Qtconsole, type logging.root.handlers. You will see that the root logger is already configured with one handler.

    As described at https://docs.python.org/2/library/logging.html#logging.basicConfig, this causes the convenience function basicConfig to do nothing.

    In contrast, the same sequence in a simple IPython terminal session shows that the root logger is not initially configured with any handlers. I don't know the reason for this difference.

    However, armed with this knowledge, you can easily change the level of the root logger with:

    logging.root.level = logging.DEBUG