Python Cherrypy Access Log Rotation

对着背影说爱祢 提交于 2019-12-07 08:12:38

问题


If I want the access log for Cherrypy to only get to a fixed size, how would I go about using rotating log files?

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


回答1:


Look at http://docs.python.org/library/logging.html.

You probably want to configure a RotatingFileHandler

http://docs.python.org/library/logging.html#rotatingfilehandler




回答2:


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

Try adding:

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

and instead of

log = app.log

maybe try

log = cherrypy.log



回答3:


The CherryPy documentation of the custom log handlers shows this very example.

Here is the slightly modified version that I use on my app:

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



回答4:


Cherrypy does its logging using the standard Python logging module. You will need to change it to use a RotatingFileHandler. This handler will take care of everything for you including rotating the log when it reaches a set maximum size.



来源:https://stackoverflow.com/questions/1601665/python-cherrypy-access-log-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!