Python Cherrypy Access Log Rotation

If I want the access log for Cherrypy to only reach a fixed size, how would I use rotating log files?

I have already tried http://www.cherrypy.org/wiki/Logging , which seems outdated or missing information.

+3
source share
4 answers
+4
source

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 Python. , RotatingFileHandler. , , .

+2

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

Source: https://habr.com/ru/post/1720694/


All Articles