Logging exceptions to a file

P

Pierre GM

All,
I need to log messages to both the console and a given file. I use the
following code (on Python 2.5)

So far so good, but I'd like to record (possibly unhandled) exceptions
in the logfile.
* Do I need to explicitly trap every single exception ?
* In that case, won't I get 2 log messages on the console (as
illustrated in the code below:
Any comments/idea welcomed
Cheers.
 
L

Lie Ryan

Pierre said:
All,
I need to log messages to both the console and a given file. I use the
following code (on Python 2.5)


So far so good, but I'd like to record (possibly unhandled) exceptions
in the logfile.
* Do I need to explicitly trap every single exception ?
* In that case, won't I get 2 log messages on the console (as
illustrated in the code below:

Any comments/idea welcomed
Cheers.

Although it is usually not recommended to use a catch-all except, this
is the case where it might be useful. JUST DON'T FORGET TO RE-RAISE THE
EXCEPTION.

if __name__ == '__main__':
try:
main():
except Exception, e:
# log('Unhandled Exception', e)
raise
 
P

Pierre GM

Although it is usually not recommended to use a catch-all except, this
is the case where it might be useful. JUST DON'T FORGET TO RE-RAISE THE
EXCEPTION.

if __name__ == '__main__':
     try:
         main():
     except Exception, e:
         # log('Unhandled Exception', e)
         raise

OK for a simple script, but the (unhandled) exceptions need to be
caught at the module level. Any idea?
 

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,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top