-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_3.py
47 lines (39 loc) · 1.65 KB
/
example_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sys
import logging
import traceback
def _exception_handler(error_type, error_value, error_traceback):
"""Log all uncaught exceptions at runtime with sys.excepthook"""
logging.exception("Uncaught exception {} {}".format(
str(error_type), str(error_value)))
tb = traceback.format_exception(
error_type, error_value, error_traceback)
traceback_string = ''
for ln in tb:
traceback_string += ln
logging.exception(traceback_string)
def configure_logging(log_filename='logfile.log', log_level=logging.DEBUG):
"""Log to terminal & file including traceback of any uncaught exceptions"""
# We use sys.excepthook to handle any uncaught exceptions
sys.excepthook = _exception_handler
logging.basicConfig(
format="%(asctime)s %(levelname)s %(message)s",
level=log_level,
handlers=[
logging.FileHandler(log_filename),
logging.StreamHandler(),
])
def main():
configure_logging(log_filename='logfile_capturing_traceback.log')
# Log some things...
logging.debug("Debugging information logged.")
logging.info("Useful information logged.")
logging.warning("Warning message logged.")
logging.error("Error message logged.")
logging.critical("Critical error message logged.")
# You should aim to catch and handle exceptions properly in your code
# and not rely on logging uncaught exceptions.
# However, having the full stack trace for any unanticipated exceptions
# can be very valuable in the event things go wrong.
raise RuntimeError('This uncaught exception causes the program to crash')
if __name__ == '__main__':
main()