473,385 Members | 1,372 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,385 software developers and data experts.

Logging: how to suppress default output when adding handlers?

I am setting up handlers to log DEBUG and above to a rotating file and
ERROR and above to console. But if any of my code calls a logger
(e.g., logging.error("foo")) before I setup my handlers, the logging
system will create a default logger that *also* emits logs, which I
can't seem to get rid of. Is there a way I can suppress the creation
of this default logger, or remove it when I 'm setting up my handlers?
Thanks.

Sample code:

import sys, logging, logging.handlers

if len(sys.argv) 1:
logging.warning("Logging before setting handlers adds unwanted default logger")

logging.getLogger().setLevel(logging.DEBUG)

console = logging.StreamHandler()
console.setLevel(logging.ERROR)
console.setFormatter(logging.Formatter('%(levelnam e)-8s %(module)s: %(message)s'))
logging.getLogger().addHandler(console)

filelog = logging.handlers.RotatingFileHandler("/tmp/logtest2.log")
filelog.setFormatter(logging.Formatter('%(asctime) s %(levelname)-8s %(module)s: %(message)s'))
filelog.setLevel(logging.DEBUG) # NOP since default above is DEBUG, but OK
logging.getLogger().addHandler(filelog)

logging.debug("TEST debug")
logging.warning("TEST warning")
logging.error("TEST error")
Sample runs, first without initial log call (good), then with one
showing the default log messages in the mix (bad); I'm showing the
console and tailing the logfile so they're mixed together but the
timestamp indicates the file logs:

chris@Bacalao:/tmp<103tail -f /tmp/logtest2.log &

chris@Bacalao:/tmp<118python /tmp/logtest2.py
2007-06-05 09:36:27,234 DEBUG logtest2: TEST debug
2007-06-05 09:36:27,234 WARNING logtest2: TEST warning
ERROR logtest2: TEST error
2007-06-05 09:36:27,239 ERROR logtest2: TEST error

chris@Bacalao:/tmp<119python /tmp/logtest2.py this gives ugly logger
WARNING:root:Logging before setting handlers adds unwanted default logger
DEBUG:root:TEST debug
2007-06-05 09:36:30,069 DEBUG logtest2: TEST debug
WARNING:root:TEST warning
2007-06-05 09:36:30,072 WARNING logtest2: TEST warning
ERROR:root:TEST error
ERROR logtest2: TEST error
2007-06-05 09:36:30,073 ERROR logtest2: TEST error
Jun 5 '07 #1
3 11498
On Jun 5, 2:44 pm, Chris Shenton <c...@shenton.orgwrote:
I am setting up handlers to log DEBUG and above to a rotating file and
ERROR and above to console. But if any of my code calls a logger
(e.g.,logging.error("foo")) before I setup my handlers, thelogging
system will create a default logger that *also* emits logs, which I
can't seem to get rid of. Is there a way I can suppress the creation
of this default logger, or remove it when I 'm setting up my handlers?
Thanks.
The default handler is created because you are calling the convenience
functions of the logging package: logging.error, etc. If you don't
want the default handler to be created, either

(a) Configure the logging system yourself before any logging call is
made (I'm not sure why you're not doing this - it could be done in
your main script before anything else happens) - or
(b) Make calls on a specific named logger, e.g.
logging.getLogger("logtest2").error("foo"), rather than
logging.error("foo") which is for casual/unsophisticated use only.

Regards,

Vinay Sajip

Jun 5 '07 #2
Vinay Sajip <vi*********@yahoo.co.ukwrites:
The default handler is created because you are calling the convenience
functions of the logging package: logging.error, etc. If you don't
want the default handler to be created, either

(a) Configure the logging system yourself before any logging call is
made (I'm not sure why you're not doing this - it could be done in
your main script before anything else happens) - or
Yeah, I think this is the cause. Unfortunately I'm using a couple
dozen files and a bunch more libraries and if they're doing a
logging.debug() or whatnot they're creating this.

Do you have any ideas how I can trace where the first call is made?
This seems a newbie question but if I have a bunch of other files
which do stuff like "from sqlalchemy import *" they might be invoking
a logging call so I'm not sure how to chase these down.
(b) Make calls on a specific named logger, e.g.
logging.getLogger("logtest2").error("foo"), rather than
logging.error("foo") which is for casual/unsophisticated use only.
I'm dreading having to be so verbose with my (copious) loggers, which
is why I was curious if there was a way to nuke any auto-created
ones. I thought calling logging.shutdown() before configuring my
loggers might do this but it didn't.

Thanks.
Jun 5 '07 #3
On Jun 5, 8:38 pm, Chris Shenton <c...@shenton.orgwrote:
Yeah, I think this is the cause. Unfortunately I'm using a couple
dozen files and a bunch more libraries and if they're doing a logging.debug() or whatnot they're creating this.
I wouldn't have thought that well-written third party libraries (like
SQLAlchemy) would log to the root logger. It's by logging to an
application or library-specific logger that people can see where the
events are being generated - logging to the root logger means a lot of
useful information is lost.
Do you have any ideas how I can trace where the first call is made?
This seems a newbie question but if I have a bunch of other files
which do stuff like "from sqlalchemy import *" they might be invoking
a logging call so I'm not sure how to chase these down.
I presume you are starting the ball rolling using one or more scripts.
As long as you are not making logging calls at import time, then you
can configure logging in your main script, e.g.

if __name__ == "__main__":
logging.basicConfigure(...)
>
I'm dreading having to be so verbose with my (copious) loggers, which
is why I was curious if there was a way to nuke any auto-created
ones. I thought callinglogging.shutdown() before configuring my
loggers might do this but it didn't.
No need to be particularly verbose. One convention is for each module
to:

# for module foo/bar/baz.py

# Near the top of the module...
import logging

logger = logging.getLogger("foo.bar.baz")

# Then, in the rest of the module...

logger.debug("Test debug output") # No more verbose than
logging.debug("Test debug output")

Even if you have a dozen files, it should be easy to do search/replace
across them without too much trouble.

Regards,

Vinay Sajip

Jun 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vinay Sajip | last post by:
Currently, if the logging module is used with no handlers configured for a logger or its parents and you try to log events with that logger, a single message is printed to sys.stderr: No...
23
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field...
10
by: Thomas Heller | last post by:
I'm about to add some logging calls to a library I have. How can I prevent that the script that uses the library prints 'No handlers could be found for logger "comtypes.client"' when the script...
16
by: Einar Høst | last post by:
Hi, I'm getting into the Trace-functionality in .NET, using it to provide some much-needed logging across dlls in the project we're working on. However, being a newbie, I'm wondering if some...
2
by: chuck | last post by:
I want to create two different loggers so that users see one output (e.g. no exception/stack traces) and others (e.g support staff) that does see stack traces via email. The code below is close...
0
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
3
by: Ross Boylan | last post by:
I would like my different threads to log without stepping on each other. Past advice on this list (that I've found) mostly says to send the messages to a Queue. That would work, but bypasses...
4
by: samwyse | last post by:
In the Python 2.5 Library Reference, section 14.5.3 (Logging to multiple destinations), an example is given of logging to both a file and the console. This is done by using logging.basicConfig()...
6
by: Thomas Heller | last post by:
I'm using the logging module in my comtypes library to log 'interesting' things that happen. In other words, the idea is if the user of the library is interested in the details that happen in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.