0

I would like to automatically include exc_info=True on every error, without having to manually code it each time I call logger.

Here's an example of manually coding it each time:

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(process)d - (levelname)s - %(message)s)

animal_dict = {'sky': 'falcon', 'earth': 'fox', 'sea': 'orca'}

try:
    animal_dict['fire']
except KeyError as err:
    logger.warning(err, exc_info=True)

Gives us the output we want, telling us it's a KeyError:

2021-11-25 20:33:31,916-11115-WARNING-'fire'
Traceback (most recent call last):
  File "<ipython-input-24-3a917f8643f5>", line 2, in <module>
    animal_dict['fire']
KeyError: 'fire'

Without exc_info=True we won't know it's a KeyError, we just get passed the string 'fire'.

I have tried adding exc_info=True into the logging.basicConfig in various ways, but cannot get it working.

Here are some of the patterns I have tried:

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(process)d - %(levelname)s - %(message)s - %(exc_info)s')
    
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(process)d - %(levelname)s - %(message)s - %(exc_info=True)s')

logging.basicConfig(level=logging.DEBUG, exc_info=True, format='%(asctime)s - %(process)d - %(levelname)s - %(message)s')
alphaomega
  • 117
  • 9

0 Answers0