3

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.

WoJ
  • 27,165
  • 48
  • 180
  • 345

1 Answers1

3

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
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677