Here's a simple way to log errors to a file in Python:
import logging
logging.basicConfig(filename='log.txt', format=logging.BASIC_FORMAT)
try:
s = 1 / 0
except Exception as ex:
logging.error(ex)
After running this code, I had a log.txt file, in the same directory as my Python file, that read:
ERROR:root:division by zero
Leave a comment