Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

Latest commit

 

History

History
42 lines (33 loc) · 1.69 KB

2013-10-01-how-to-log-exceptions-in-python.md

File metadata and controls

42 lines (33 loc) · 1.69 KB
layout title date categories slug
post
How to log exceptions in Python
2013-10-01 10:30
python
exceptions
how-to-log-exceptions-in-python

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:

<script src="https://gist.github.com/zonca/6782980.js"></script>

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