How to log detailed Python error info?

How can I log a Python error with detailed debug information?

I am currently logging Python exception messages to a log file using logging.error. For example:

import logging

try: 1 / 0 except ZeroDivisionError as e: logging.error(e) # Logs: ERROR:root:division by zero

While this works, it only logs the exception string. I want to log more detailed information about the exception, such as the line number or stack trace, to better understand where the error occurred.

How can I achieve this using python logging exception?

Hey All!

Use exc_info=True with logging.error. The exc_info=True argument logs the stack trace alongside the error message.

import logging

# Configure logging
logging.basicConfig(level=logging.ERROR)

try:
    1 / 0
except ZeroDivisionError as e:
    logging.error("An error occurred", exc_info=True)

Output in log:

ERROR:root:An error occurred  
Traceback (most recent call last):  
  File "example.py", line 7, in <module>  
    1 / 0  
ZeroDivisionError: division by zero  

This method provides a simple and effective way to include the full stack trace while logging errors.

Hey @heenakhan.khatri

Here is your answer to the Question:-

Building on Tom’s approach, if you want more control over how the stack trace is formatted, you can use the traceback.format_exc function. This allows you to manually format and log the exception for greater customization.

import logging
import traceback

# Configure logging
logging.basicConfig(level=logging.ERROR)

try:
    1 / 0
except ZeroDivisionError as e:
    error_message = traceback.format_exc()
    logging.error("An error occurred:\n%s", error_message)

Output in log:

ERROR:root:An error occurred:  
Traceback (most recent call last):  
  File "example.py", line 7, in <module>  
    1 / 0  
ZeroDivisionError: division by zero  

This approach is especially useful when you want to format or modify the trace before logging it.

Best Explanation given by both of you @prynka.chatterjee @joe-elmoufak

Here is the another explanation:-

To enhance Toby’s suggestion, Python’s logging library provides a built-in shortcut for logging exceptions with the logger.exception method. This method automatically logs the error message and stack trace, making it more concise and less prone to errors.

import logging

# Configure logging
logging.basicConfig(level=logging.ERROR)

try:
    1 / 0
except ZeroDivisionError:
    logging.exception("An exception occurred")

Output in log:

ERROR:root:An exception occurred  
Traceback (most recent call last):  
  File "example.py", line 7, in <module>  
    1 / 0  
ZeroDivisionError: division by zero  

By using logger.exception, you get both clarity and brevity in your error-handling logic. All three methods effectively log detailed error and traceback information, but this one is particularly handy when you want simplicity without losing essential details."