How do I log a Python error with debug information

0 votes

I am printing Python exception messages to a log file with logging.error:

import logging
except ZeroDivisionError as e:
    logging.error(e)  # ERROR:root:division by zero

Is it possible to print more detailed information about the exception and the code that generated it than just the exception string? Things like line numbers or stack traces would be great.

May 11, 2020 in Python by kartik
• 37,510 points

You could probably use traceback for this.

import traceback

def log_traceback(ex, ex_traceback=None):
    if ex_traceback is None:
        ex_traceback = ex.__traceback__
    tb_lines = [ line.rstrip('\n') for line in
                 traceback.format_exception(ex.__class__, ex, ex_traceback)]

Here's how you can use it in Python 3:

    x = get_number()
except Exception as ex:

1 answer to this question.

0 votes


In most applications, you won't be calling logging.exception(e) directly. Most likely you have defined a custom logs specific for your application or module like this:

# Set the name of the app or module
my_logger = logging.getLogger('NEM Sequencer')
# Set the log level

# Let's say we want to be fancy and log to a graylog2 log server
graylog_handler = graypy.GELFHandler('some_server_ip', 12201)

In this case, just use the logger to call the exception(e) like this:

except ZeroDivisionError, e:
answered May 11, 2020 by Niroj
• 82,720 points

Related Questions In Python

0 votes
1 answer

How do I generate some random numbers with a distribution using Python?

scipy.stats.rv_discrete is what you ned IMHO. You can supply ...READ MORE

answered Oct 31, 2018 in Python by Anirudh
• 2,080 points

edited Dec 14, 2018 by Anirudh 278 views
0 votes
1 answer

How do I get a random number with a float range in python?

Use random.uniform(a, b): Returns a random floating point ...READ MORE

answered May 27, 2019 in Python by Kiran
0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,870 points
0 votes
1 answer

How do I install a Python package with a .whl file?

I just used the following which was ...READ MORE

answered Nov 20, 2020 in Python by Gitika
• 65,870 points
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,058 views
0 votes
0 answers
0 votes
1 answer

How Can I log a Python error with debug information?

Hii, Using exc_info options may be better, to allow you ...READ MORE

answered May 27, 2020 in Python by Niroj
• 82,720 points
0 votes
1 answer

How do I merge two dictionaries in a single expression in Python?

Hello, For dictionaries x and y, z becomes a shallowly merged dictionary with ...READ MORE

answered Apr 13, 2020 in Python by Niroj
• 82,720 points