如何忽略在python登录期间生成的异常? [英] How to ignore the exception which are generated during the logging in python?

查看:119
本文介绍了如何忽略在python登录期间生成的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用大型python模块(Python 2.6.6),并在数千个地方进行日志记录,并且我正在使用python的logger模块. 现在,我想忽略在日志记录期间生成的异常. 一种可能的基本方案是

I am working in a big python module(Python 2.6.6) and doing logging in thousand of places and i am using logger module of python. Now, i want to ignore the exception, which are generated during the logging. One of the basic scenario which could be possible is

import glob
import logging
import logging.handlers

LOG_FILENAME = '/home/abc/logging_rotatingfile_example.out'
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)

# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(
              LOG_FILENAME, maxBytes=20, backupCount=5)
my_logger.addHandler(handler)
# Log some messages
for i in range(20):
    my_logger.debug('i = %d' % i)

现在,假设在my_logger.debug('i = %d' % i)期间,如何从系统中删除文件或更改权限,并且由于日志文件上的磁盘故障,磁盘已满等而导致记录器无法记录并在以下位置生成异常屏幕. 它是分布式系统,n个进程正在使用记录器模块.请注意,由于日志记录异常(由于磁盘故障,磁盘已满等),不应中止该过程. 因此,我希望忽略该异常.

Now suppose, during the my_logger.debug('i = %d' % i) some how the file is deleted from the system or permissions are changed and logger couldn't able to log due to disk failure, disk full etc. on the log file and generate the exception on the screen. It is distributed system and n number of process are using logger module. Please note process should not be aborted due to logging exception(due to disk failure, disk full etc.). So i want that exception to be ignored.

注意: 现在,我知道在python记录器上制作包装器类并使用try-except记录器功能的一种方法. 但是我有不同的要求,并且我不想使用这种方式,是否还有其他方法可以做到这一点,或者是否有python logger的任何对我的病情有用的属性?

NOTE: Now one of the way i know to make wrapper class on python logger and use try-except for the logger function. But i have different requirements and i dont want to use that way so ,is there any other way to do this or any attribute of python logger which can be useful for my condition?

推荐答案

您无需忽略异常,因为logging模块已经为您处理了这些异常.

You do not need to ignore exceptions, because the logging module already handles these for you.

所有文件操作(发出日志,旋转等)均由try..except Exception块保护.如果发生异常,则 logging.Handler.handeError()方法会写sys.stderr的例外.您可以将logging.raiseExceptions设置为False以使这些内容静音:

All file operations (log emitting, rotation, etc.) are guarded with try..except Exception blocks. If exceptions occur, the logging.Handler.handeError() method will write the exception to sys.stderr instead. You can set logging.raiseExceptions to False to silence these:

import logging

logging.raiseExceptions = False

引用文档:

emit()调用期间遇到异常时,应从处理程序中调用此方法.如果模块级属性raiseExceptionsFalse,则异常将被忽略.这是日志系统最需要的-大多数用户不会关心日志系统中的错误,他们对应用程序错误更感兴趣.但是,您可以根据需要将其替换为自定义处理程序.指定的记录是发生异常时正在处理的记录. (raiseExceptions的默认值为True,因为它在开发过程中更有用).

This method should be called from handlers when an exception is encountered during an emit() call. If the module-level attribute raiseExceptions is False, exceptions get silently ignored. This is what is mostly wanted for a logging system - most users will not care about errors in the logging system, they are more interested in application errors. You could, however, replace this with a custom handler if you wish. The specified record is the one which was being processed when the exception occurred. (The default value of raiseExceptions is True, as that is more useful during development).

如果无法创建传递给RotatingFileHandler的文件名,则引发的异常不会在记录期间发生,但是会在创建处理程序时发生.您要么必须在那里处理该异常,要么在创建处理程序时将delay=True设置为将打开文件的时间推迟到记录时间:

If the filename you passed to RotatingFileHandler cannot be created, then the exception raised doesn't happen during logging however, it takes place while creating the handler. You'll either have to handle that exception there and then, or set delay=True when creating the handler to postpone opening the file until logging time:

handler = logging.handlers.RotatingFileHandler(
    LOG_FILENAME, maxBytes=20, backupCount=5, delay=True)

您所使用的归结为是否消失,您会发现在LOG_FILENAME中提供错误的路径是否是应用程序错误.

What you use comes down to wether or not you see providing an incorrect path in LOG_FILENAME is an application error or not at that point.

这篇关于如何忽略在python登录期间生成的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆