2

Ultimately i'm trying to write a custom logging module to log the events of some unmanned daily scripts, and currently i'm looking at how to log exceptions.

Several other SO questions have suggested replacing sys.excepthook with a function to achieve this, but I couldn't get this to work as intended inside my module so now i'm just trying to get a basic example working so I can start to understand what's happening here.

in python, how ensure any exceptions are logged with logger?

The above question outlines some code they've used to achieve this which is similar/identical to acceptable answers in other SO questions, but none seem to work for me.

Here is my code:

import logging
import sys


logger = logging.getLogger('myapp logger')
logger.setLevel(logging.INFO)
file_log_handler = logging.FileHandler('myapp.log')
logger.addHandler(file_log_handler)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_log_handler.setFormatter(formatter)
logger.addHandler(file_log_handler)


def _excepthook(exctype, exc, tb):
    logger.error("An unhandled exception occurred.", exc_info=(exctype, exc, tb))

sys.excepthook = _excepthook


logger.debug("Hello")
5/0 # error
logger.debug("what")

I'm expecting this to print the details of the ZeroDivisionError to 'myapp.log' but when running from within VS Code, the ZeroDivisionError exception is raised as normal and nothing is printed to 'myapp.log'.

Does anyone know where i'm going wrong or how I should be thinking about/approaching this?

A super simple example of replacing sys.excepthook to do anything other than its default behavior would be greatly appreciated.

BioData41
  • 862
  • 9
  • 15

1 Answers1

1

I was trying to test this out in Jupyter Notebook and had the same problem. Can't override sys.excepthook there or in VS Code, it appears.

Googling a little further, I found this SO thread, which explains that IPython replaces sys.excepthook every time you execute a line of code.

This could perhaps be the source of your problem, if your default python interpreter on your system is based on IPython. In that case, you could try pointing VS Code to the path of an alternate interpreter under File >> Preference >> Settings >> Extensions >> Python >> Default Interpreter Path in VS Code.

enter image description here

Also, note that your logger.debug() commands won't write to your log file unless you set the logger level to "DEBUG", because "DEBUG" is at a lower level than "INFO":
enter image description here

-----------UPDATE-----------------

On that SO thread I referenced above, further down there is an answer from "Michał Jabłoński", that might work for you, if you are using an IPython interpreter. It worked for me in Jupyter Notebook.
This worked for me:

import logging
import sys
from IPython import get_ipython
ip = get_ipython()


logger = logging.getLogger('myapp logger')
logger.setLevel(logging.DEBUG)
file_log_handler = logging.FileHandler('myapp.log')
logger.addHandler(file_log_handler)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_log_handler.setFormatter(formatter)
logger.addHandler(file_log_handler)


def exception_handler(self, etype, evalue, tb, tb_offset=None):
    logger.error("An unhandled exception occurred.", exc_info=(etype, evalue, tb))

ip.set_custom_exc((Exception,), exception_handler)


logger.debug("Hello")
5/0 # error
logger.debug("what")
BioData41
  • 862
  • 9
  • 15
  • Brillant, thanks for looking. Your answer prompted me to run my initial example outside of VS Code and it worked perfectly. I couldn't get your example to work inside of VS Code, but this is actually better behavior as I can still see the errors while developing but they will be logged when running daily. – jacks-music Dec 24 '21 at 10:05