Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...