logging question

G

Guest

I going through my first attempt at using the new logging facilities. I
would like to have log output go to both a file and std error and perhaps
email as well. Is there a handler that allows this sort of multiple outputs
capture or will a custom handler have to be written?
 
J

Jeff Shannon

I going through my first attempt at using the new logging facilities. I
would like to have log output go to both a file and std error and perhaps
email as well. Is there a handler that allows this sort of multiple outputs
capture or will a custom handler have to be written?

Each logger can have multiple handlers attached to it, with each of the
handlers having their own threshold. You can easily create a file
handler, a stream (stdout) handler, and an email handler, all attached
to your (root) logger. Any logged messages should go to all of the
attached handlers. But if you, for example, give the email handler a
threshold of 'critical', then 'info' and 'error' messages will go to the
file and to stdout but won't get emailed.

Jeff Shannon
Technician/Programmer
Credit International
 
C

Chuck

Each logger can have multiple handlers attached to it, with each of the
handlers having their own threshold. You can easily create a file
handler, a stream (stdout) handler, and an email handler, all attached
to your (root) logger. Any logged messages should go to all of the
attached handlers. But if you, for example, give the email handler a
threshold of 'critical', then 'info' and 'error' messages will go to the
file and to stdout but won't get emailed.

Go it...

import logging

log = logging.getLogger("app.log")
fmt = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
h1 = logging.StreamHandler()
h1.setFormatter(fmt)
h2 = logging.FileHandler('.//app.log')
h2.setFormatter(fmt)
log.addHandler(h1)
log.addHandler(h2)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top