Logger vs. Handler log levels

John J. Lee jjl at pobox.com
Fri Jun 22 00:53:31 CEST 2007

steveg at moregruel.net (Steve Greenland) writes:

> Apparently I don't understand logging levels. The code:
>     import logging, logging.handlers
>     logging.basicConfig(level=logging.WARNING)
>     newlog = logging.handlers.TimedRotatingFileHandler(
>         filename='/home/steveg/logtest.log',
>         when="midnight")
>     newlog.setLevel(logging.INFO)
>     logging.getLogger().addHandler(newlog)
>     logging.info("Message from logtest")
>     logging.debug("Debug message from logtest")
> doesn't print on the console (as expected), but also doesn't add the
> first message to the file. Changing the basicConfig() call to set the
> level to 'level.DEBUG' shows both messages on the console, and only the
> first in the file, which is what I did expect.

Not sure why you'd expect to see console output regardless of the
logger / handler issue -- INFO and DEBUG are lower in priority than
WARNING, and you've set the (logger) level to WARNING with the
logging.basicConfig() call (indicating you're only interested in
things at least that serious, not mere INFOrmational messages).

> I suspect my confusion is the distinction between "handler" and
> "logger". It appears that the logger won't pass any messages lower than
> its level to any of its handlers. If I read the docs, what I need is to
> do is have the root logger left at NOTSET, but then there doesn't seem
> to be any way to set the level on the default stream handler.


> Have I diagnosed the problem correctly? Is the only (or at least,
> correct) solution to ignore the default handler and create my own
> console and file handlers, setting levels as desired?

Yes, I think so.


More information about the Python-list mailing list