如何防止 python 日志记录模块引发错误停止执行停机、永久重试和缓冲事件
How do I prevent python logging module from raising errors stopping execution on downtime, permanently retry and buffer events
我有一个 Python 进程,其输出与其他处理程序一起通过 tcp 记录到系统日志服务器。系统日志服务器可能会不时发生一些停机时间。无论记录机制如何,我都需要 运行 的脚本。是否有任何 options/commonly 使用的(或内置的)库来缓冲我可能丢失的日志并无限重试,或者我是否需要为处理缓冲的日志记录编写自定义包装器 class并重试?
当我停止系统日志服务器时出现问题,在这种情况下任何 "logger" 语句都会引发错误并停止脚本执行。
import logging
from logging.handlers import SysLogHandler
...
logger = logging.getLogger()
handler = SysLogHandler(address=syslog_address, socktype=socket.SOCK_STREAM)
logger.addHandler(handler)
...
logger.info("Some statements all over my code I want logged and buffered if possible but I do not want to raise exceptions stopping execution and I don't want to repeat myself wrapping them all in try/except blocks"
python 为此提供的内置功能是 QueueHandler. What you do is move the SysLogHandler to a separate thread (or process) with a QueueListener and replace it with a QueueHandler in the application. This way you are insulating your app from failures caused by the syslog and queue messages are automatically buffered. Implementing infinite retry is pretty easy with a Queue,只是将失败的任务放回去。
我有一个 Python 进程,其输出与其他处理程序一起通过 tcp 记录到系统日志服务器。系统日志服务器可能会不时发生一些停机时间。无论记录机制如何,我都需要 运行 的脚本。是否有任何 options/commonly 使用的(或内置的)库来缓冲我可能丢失的日志并无限重试,或者我是否需要为处理缓冲的日志记录编写自定义包装器 class并重试?
当我停止系统日志服务器时出现问题,在这种情况下任何 "logger" 语句都会引发错误并停止脚本执行。
import logging
from logging.handlers import SysLogHandler
...
logger = logging.getLogger()
handler = SysLogHandler(address=syslog_address, socktype=socket.SOCK_STREAM)
logger.addHandler(handler)
...
logger.info("Some statements all over my code I want logged and buffered if possible but I do not want to raise exceptions stopping execution and I don't want to repeat myself wrapping them all in try/except blocks"
python 为此提供的内置功能是 QueueHandler. What you do is move the SysLogHandler to a separate thread (or process) with a QueueListener and replace it with a QueueHandler in the application. This way you are insulating your app from failures caused by the syslog and queue messages are automatically buffered. Implementing infinite retry is pretty easy with a Queue,只是将失败的任务放回去。