Python Logging module not respecting setLevel below '30' (Warning)?
Question: I'm at a bit of a loss here. I could swear that I had this working earlier
last week. Today I've returned to it and can't seem to get logging to work.

In the below sample script, I hope to show a relatively complete demonstration
of my issue.

**test.py**

    
    
    import logging
    logger = logging.getLogger()
    print("Logger currently at level '{0}'".format(logger.getEffectiveLevel()))
    logger.warning("Testing Warning")
    logger.setLevel(60)
    print("Logger currently at level '{0}'".format(logger.getEffectiveLevel()))
    logger.error("Testing Error")
    logger.setLevel(50)
    print("Logger currently at level '{0}'".format(logger.getEffectiveLevel()))
    logger.error("Testing Error")
    logger.setLevel(40)
    print("Logger currently at level '{0}'".format(logger.getEffectiveLevel()))
    logger.error("Testing Error")
    logger.setLevel(30)
    print("Logger currently at level '{0}'".format(logger.getEffectiveLevel()))
    logger.info("Testing Info")
    logger.setLevel(20)
    print("Logger currently at level '{0}'".format(logger.getEffectiveLevel()))
    logger.info("Testing Info")
    logger.setLevel(10)
    print("Logger currently at level '{0}'".format(logger.getEffectiveLevel()))
    logger.info("Testing Info")
    logger.setLevel(0)
    print("Logger currently at level '{0}'".format(logger.getEffectiveLevel()))
    logger.info("Testing Info")
    

**output**

    
    
    Logger currently at level '30'
    Testing Warning
    Logger currently at level '60'
    Logger currently at level '50'
    Logger currently at level '40'
    Testing Error
    Logger currently at level '30'
    Logger currently at level '20'
    Logger currently at level '10'
    Logger currently at level '0'
    

I have no idea why the logging module is acting differently, considering I'm
using [this](https://docs.python.org/3.3/howto/logging-cookbook.html#using-
logging-in-multiple-modules) official documentation to try and make it happen.
You can see near the end how it's blatantly ignoring the level for `logger`.
Am I somehow bypassing `logger` and instead logging against a default? I don't
understand.

Thanks!


Answer: You forgot to add a handler to your logger. On Python 3.3 (and later), if no
handler is specified, an internal ["handler of last
resort"](https://docs.python.org/3.3/library/logging.html#logging.lastResort),
which has a level of `WARNING`, is used. So even though your messages are sent
through the logger, they are discarded by the internal handler (which is only
there to catch misconfigurations). On Python 2.x, you would have got a one-off
message printed to `stderr` \- `No handlers could be found for logger "root"`.

If you add a line

    
    
    logger.addHandler(logging.StreamHandler())
    

before the first print, you will get behaviour like this:

    
    
    Logger currently at level '30'
    Testing Warning
    Logger currently at level '60'
    Logger currently at level '50'
    Logger currently at level '40'
    Testing Error
    Logger currently at level '30'
    Logger currently at level '20'
    Testing Info
    Logger currently at level '10'
    Testing Info
    Logger currently at level '0'
    Testing Info
    

**Update:** There is no need to "add a handler manually" - this was just the
easiest way to illustrate what the problem was. You can use `basicConfig()` to
configure logging simply (console or file, limited options) or
use[`dictConfig()`](https://docs.python.org/2/library/logging.config.html#logging.config.dictConfig)
to configure more flexibly. These methods work in the same way across Python
2.7 and 3.2 (and later).

