473,222 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 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 4136
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nermin Brgulja | last post by:
Hi, I am using jdk1.4 Logger with a FileHandler for logging in a web application, and have two questions regarding logging: The first question is: I've created an Logger and mapped it to an...
1
by: peiyin.li | last post by:
Hi, I want to have two loggers that logs to two different files. Here is what I have: import logging import logging.handlers project1Handler = logging.handlers.RotatingFileHandler(...
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
1
by: David Bear | last post by:
I attempted to use the logger module per the instructions in http://docs.python.org/lib/minimal-example.html I tried a couple of differnet levels. Nothing appeared in any logs. Has this module...
2
by: Robert | last post by:
Hi all I have been trying to set up a framework with logging implemented using the built in Python logging module. For better or worse, I have decided to base the logger names on the module...
10
by: Gary Jefferson | last post by:
Suppose I have 3 modules that belong to a project, 'A', 'A.a' and 'B', and each module has its own logger, created with: module1logger = logging.getLogger('project.A') and module2logger =...
5
by: CedricCicada | last post by:
Greetings! I want to write messages into the Windows event log. I found sevicemanager, but the source is always "Python Service", and I'd like to be a bit more descriptive. Poking around on...
2
by: puzzlecracker | last post by:
Looking for a basic logger that would display timestamp at various levels Info: my message Warning : Error: etc any suggestions and pointers?
3
by: ASh | last post by:
Hi, I have this source: import logging import logging.config logging.config.fileConfig("logging.properties") log = logging.getLogger("qname") log.debug("message")
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.