How to log exceptions in Python

python
Published

October 1, 2013

Sometimes it is useful to just catch any exception, write details to a log file and continue execution.

In the Python standard library, it is possible to use the logging and exceptions modules to achieve this. First of all, we want to catch any exception, but also being able to access all information about it:

try:
    my_function_1()
except exception.Exception as e:
    print e.__class__, e.__doc__, e.message

Then we want to write those to a logging file, so we need to setup the logging module:

import logging
logging.basicConfig( filename="main.log",
                     filemode='w',
                     level=logging.DEBUG,
                     format= '%(asctime)s - %(levelname)s - %(message)s',
                   )

In the following gist everything together, with also function name detection from Alex Martelli:

Here the output log:

2013-10-01 11:32:56,466 - ERROR - Function my_function_1() raised <type 'exceptions.IndexError'> (Sequence index out of range.): Some indexing error
2013-10-01 11:32:56,466 - ERROR - Function my_function_2() raised <class 'my_module.MyException'> (This is my own Exception): Something went quite wrong
2013-10-01 11:32:56,466 - ERROR - Function my_function_1_wrapper() raised <type 'exceptions.IndexError'> (Sequence index out of range.): Some indexing error