简体   繁体   中英

How do I log errors?

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.

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.

在此处输入图像描述

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" :
在此处输入图像描述

-----------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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM