views:

158

answers:

3

I've been trying to get this working, but for some reason it's giving me errors...

How can I log my python errors?

try:
    pass #CODE HERE
except:
    pass #LOG TRACEBACK ERROR ...whatever that error may be
A: 

Check out the logging module in the standard library: it has a wide variety of logging options and is fairly straight forward to use.

mavnn
+3  A: 

Heres a simple example taken from the python 2.6 documentation:

import logging
LOG_FILENAME = '/tmp/logging_example.out'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)

logging.debug('This message should go to the log file')
rogeriopvl
+3  A: 
import logging
LOG_FILENAME = '/tmp/logging_example.out'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)

logging.debug('This message should go to the log file')

try:
   run_my_stuff()
except:
   logging.exception('Got exception on main handler')
   raise

Now looking at the log file, /tmp/logging_example.out:

DEBUG:root:This message should go to the log file
ERROR:root:Got exception on main handler
Traceback (most recent call last):
  File "/tmp/teste.py", line 9, in <module>
    run_my_stuff()
NameError: name 'run_my_stuff' is not defined
nosklo
perfect.!!!!!!!!!
TIMEX