473,386 Members | 2,050 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,386 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 4159
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")
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.