Logger vs. Handler log levels

S

Steve Greenland

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.

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?

Thanks,
Steve
 
J

John J. Lee

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.
Yup.


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.



John
 
S

Steve Greenland

According to John J. Lee said:
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).

Yes, that's why I added "(as expected)" :) Oh, duh, I see, it can be
parsed to mean the opposite of what I intended. What I meant was "As
expected, there was no console output."

Anyway, thanks for your confirmations.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top