如果我想让Cherrypy的访问日志只保留固定大小,我该如何使用轮换日志文件?
我已经尝试过http://www.cherrypy.org/wiki/Logging,但这似乎已经过时或缺少信息。
我已经尝试过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
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 logging模块进行日志记录。您需要将其更改为使用RotatingFileHandler。此处理程序将为您处理所有内容,包括在达到设置的最大大小时旋转日志。