Python Cherrypy 访问日志轮转

4
如果我想让Cherrypy的访问日志只保留固定大小,我该如何使用轮换日志文件?
我已经尝试过http://www.cherrypy.org/wiki/Logging,但这似乎已经过时或缺少信息。
4个回答

4

3

我已经尝试过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

3

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

2

Cherrypy使用标准的Python logging模块进行日志记录。您需要将其更改为使用RotatingFileHandler。此处理程序将为您处理所有内容,包括在达到设置的最大大小时旋转日志。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接