【发布时间】:2010-12-08 18:28:19
【问题描述】:
如果我希望 Cherrypy 的访问日志仅达到固定大小,我将如何使用轮换日志文件?
我已经尝试过http://www.cherrypy.org/wiki/Logging,它似乎已经过时,或者缺少信息。
【问题讨论】:
如果我希望 Cherrypy 的访问日志仅达到固定大小,我将如何使用轮换日志文件?
我已经尝试过http://www.cherrypy.org/wiki/Logging,它似乎已经过时,或者缺少信息。
【问题讨论】:
看http://docs.python.org/library/logging.html。
你可能想要配置一个 RotatingFileHandler
http://docs.python.org/library/logging.html#rotatingfilehandler
【讨论】:
我已经尝试过http://www.cherrypy.org/wiki/Logging,看起来 已过期,或缺少信息。
尝试添加:
import logging
import logging.handlers
import cherrypy # you might have imported this already
而不是
log = app.log
可以试试
log = cherrypy.log
【讨论】:
custom log handlers 的 CherryPy 文档显示了这个例子。
这是我在应用中使用的稍作修改的版本:
import logging
from logging import handlers
def setup_logging():
log = cherrypy.log
# Remove the default FileHandlers if present.
log.error_file = ""
log.access_file = ""
maxBytes = getattr(log, "rot_maxBytes", 10000000)
backupCount = getattr(log, "rot_backupCount", 1000)
# Make a new RotatingFileHandler for the error log.
fname = getattr(log, "rot_error_file", "log\\error.log")
h = handlers.RotatingFileHandler(fname, 'a', maxBytes, backupCount)
h.setLevel(logging.DEBUG)
h.setFormatter(cherrypy._cplogging.logfmt)
log.error_log.addHandler(h)
# Make a new RotatingFileHandler for the access log.
fname = getattr(log, "rot_access_file", "log\\access.log")
h = handlers.RotatingFileHandler(fname, 'a', maxBytes, backupCount)
h.setLevel(logging.DEBUG)
h.setFormatter(cherrypy._cplogging.logfmt)
log.access_log.addHandler(h)
setup_logging()
【讨论】:
Cherrypy 使用标准 Python 日志记录模块进行日志记录。您需要将其更改为使用RotatingFileHandler。此处理程序将为您处理所有事情,包括在日志达到设置的最大大小时旋转日志。
【讨论】: