Python Example | Capture Exception Message Using logger.exception()

This python example code shows you how to capture exception message using logger.exception() in python.

import logging

logger = logging.getLogger()

try:
    x = 1/0
except Exception as e:
    logger.exception('Exception occurred while code execution: ' + str(e))

Run this code, this code will output:

Exception occurred while code execution: division by zero
Traceback (most recent call last):
  File "<ipython-input-27-912703271615>", line 5, in <module>
    x = 1/0
ZeroDivisionError: division by zero