William Troup
William Troup

Reputation: 13131

Python Cherrypy Access Log Rotation

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.

Upvotes: 4

Views: 2620

Answers (4)

stenci
stenci

Reputation: 8481

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

Upvotes: 3

e1i45
e1i45

Reputation: 1589

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

Upvotes: 3

Michael Dillon
Michael Dillon

Reputation: 32392

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.

Upvotes: 2

S.Lott
S.Lott

Reputation: 391848

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

You probably want to configure a RotatingFileHandler

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

Upvotes: 4

Related Questions