471,330 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Logger vs. Handler log levels

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
--
Steve Greenland
The irony is that Bill Gates claims to be making a stable operating
system and Linus Torvalds claims to be trying to take over the
world. -- seen on the net
Jun 21 '07 #1
2 3947
st****@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.
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
Jun 21 '07 #2
According to John J. Lee <jj*@pobox.com>:
st****@moregruel.net (Steve Greenland) writes:
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).
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
--
Steve Greenland
The irony is that Bill Gates claims to be making a stable operating
system and Linus Torvalds claims to be trying to take over the
world. -- seen on the net
Jun 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Nermin Brgulja | last post: by
1 post views Thread by peiyin.li | last post: by
1 post views Thread by David Bear | last post: by
2 posts views Thread by Robert | last post: by
10 posts views Thread by Gary Jefferson | last post: by
5 posts views Thread by CedricCicada | last post: by
2 posts views Thread by puzzlecracker | last post: by
3 posts views Thread by ASh | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.