Basic Error Logging in Python

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

Comments

Leave a comment

What color are brown eyes? (spam prevention)
Submit
Code under MIT License unless otherwise indicated.
© 2020, Downranked, LLC.