How can I log a stack trace to a file instead of the console?

Responsive Ad Header

Question

Grade: Education Subject: Support
How can I log a stack trace to a file instead of the console?
Asked by:
61 Viewed 61 Answers

Answer (61)

Best Answer
(228)
Configure the `logging` module to use a `FileHandler`. For example: `import logging; logging.basicConfig(filename='error.log', level=logging.ERROR); try: ... except Exception as e: logging.exception("An error occurred: %s", e)`.