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

logging module question

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 but I still
get console output on the email logger from the "root" logger. How do
I get rid of it?
import logging
import logging.handlers
def main():
console = logging.StreamHandler()
console.setFormatter(logging.Formatter('%(asctime) s %(message)s'))
console.setLevel(logging.CRITICAL)
logging.getLogger('').addHandler(console)

mail = logging.handlers.SMTPHandler('mail.vw.com',
'm********@mycompany.com',
'm********@mycompany.com',
'Data Processing Error at
location: %s' % "Chuck's Desk")
mail.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
logging.getLogger('mail').addHandler(mail)

try:
raise Exception("foobar")
except Exception, e:
logging.getLogger('').error(e)
print "Done logging to console"
logging.getLogger('mail').exception(e)

if __name__ == "__main__":
main()

Jan 23 '06 #1
2 2176
Ok, so I've figured this out (see below),

mail = logging.handlers.SMTPHandler('mail.vw.com',
'c**************@gedas.com',
'c**************@gedas.com',
'Data Processing Error at
location: %s' % "Chuck's Desk")
mail.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
ml = logging.getLogger('mail')
ml.addHandler(mail)
ml.propagate = 0 ## <---------------do this to turn off
propagation of error reporting

but now I'd like to understand the design rational behind having each
logger in the object hierarchy log the same output by default. Anyone?

Jan 23 '06 #2
> but now I'd like to understand the design rational behind having each
logger in the object hierarchy log the same output by default. Anyone?


Loggers are different to handlers. Loggers map to areas of the
application, handlers map to output destinations. Loggers form a
hierarchy based on names - "myapp" is a parent logger of
"myapp.archiving" or "myapp.workflow", for example. All loggers are
descendants of a root logger.

Logging events are not passed to all loggers, but are by default passed
to the handlers of the logger which logs the event, and then up the
hierarchy to the handlers of ancestor loggers. Both handlers and
loggers can be set to filter by level, or (by using Filters) can filter
according to custom criteria. You can also use custom Formatters to
format the output - for example, a formatter which does not send
traceback information to the output can be set (for a particular
handler).

See PEP 282 for background information on the logging scheme. You can
also look at Apache logging (log4j, log4net etc.) and java.util.logging
which adopt a similar approach (experience having shown that this is a
good approach).

Regards,

Vinay Sajip

Jan 25 '06 #3

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

Similar topics

5
by: j vickroy | last post by:
Hello, I'm trying to understand the behavior of the Python 2.3 logging module (MS Windows 2k) with regard to RotatingFileHandler. The following script illustrates a puzzling problem. What is...
6
by: Ville Vainio | last post by:
Just posting this for the sake of google: Like everyone else, I figured it's time to start using the 'logging' module. I typically want to dump "info" level (and up) log information to...
2
by: rh0dium | last post by:
Hi all, So I have a slice of code which calls other python code. I have started to take a real liking to the logging module, but I want to extend this into the called python code. I have 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...
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: Chris Shenton | last post by:
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...
6
by: Vyacheslav Maslov | last post by:
Hi all! I have many many many python unit test, which are used for testing some remote web service. The most important issue here is logging of test execution process and result. I strongly...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
1
by: ludvig.ericson | last post by:
Quote from the docs: FORMAT = "%(asctime)-15s %(clientip)s %(user)-8s %(message)s" logging.basicConfig(format=FORMAT) d = {'clientip': '192.168.0.1', 'user': 'fbloggs'}...
4
by: Matthew Wilson | last post by:
I'm working on a package that uses the standard library logging module along with a .cfg file. In my code, I use logging.config.fileConfig('/home/matt/mypackage/matt.cfg') to load in the...
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:
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.