I run a headless and unmanned server which logs everything to a remote server. The application I run there also does this via a SysLogHandler
. It works fine.
I have cases where the program crashes (in places not handled by a try: except:
) and I would like to log that way the traceback as well. Is this at all possible?
I am specifically talking about unexpected tracebacks , I know that I can log exceptions in an except:
clause.
You could make a custom excepthook
.
When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object
import sys
import logging
logger = ...
def excepthook(type_, value, traceback):
logger.exception(value)
# call the default excepthook
sys.__excepthook__(type_, value, traceback)
sys.excepthook = excepthook