Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (19k points)

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

import logging

try:

   1/0

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.

1 Answer

0 votes
by (106k points)

For logging a Python error with debug information you can use exc_info options may be better, to allow you to choose the error level (if you use exception, it will always show error):

try:

  # write your code here.

except Exception as e:

  logging.fatal(e, exc_info=True)

image

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...