473,326 Members | 2,126 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,326 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 2172
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.