Python Cherrypy 访问日志轮转

2024-04-26 00:07:02 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我希望Cherrypy的访问日志只有一个固定的大小,那么如何使用循环日志文件呢?在

我已经尝试过http://www.cherrypy.org/wiki/Logging,它似乎过时了,或者缺少信息。在


Tags: 文件org信息httploggingwwwwikicherrypy
3条回答

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

I've already tried http://www.cherrypy.org/wiki/Logging, which seems out of date, or has information missing.

尝试添加:

import logging
import logging.handlers
import cherrypy # you might have imported this already

而不是

^{pr2}$

也许可以试试

log = cherrypy.log

相关问题 更多 >