Search code examples
pythonpython-2.7cherrypy

cherrypy: python how to create separate request log files for everyday with the help of log rotator


i want to create seperate log file for access and error in cherrypy. i read http://docs.cherrypy.org/dev/refman/_cplogging.html , but i am not getting to write code to implement logging.

i read on more link Python Cherrypy Access Log Rotation.

plz provide code and document how to implement logging in cherrypy plz help me out by providing a sample code

i hv one hello.py file

import cherrypy

class HelloWorld: def index(self): return "Hello world!" index.exposed = True

@cherrypy.expose
def link(self):
    #print HelloWorld.c

    #print self.j
    return "link"

@cherrypy.expose
def link2(self):
    return "link2"

if name == 'main' : cherrypy.quickstart(HelloWorld())

whats changes are needed to implement logging please provide code..


Solution

  • Try this...

    import logging
    from cherrypy import _cplogging
    from logging import handlers
    import cherrypy
    
    class HelloWorld:
        @cherrypy.expose 
        def index(self):
            return "Hello world!" 
    
        @cherrypy.expose
        def link(self):
            #print HelloWorld.c
    
            #print self.j
            return "link"
    
        @cherrypy.expose
        def link2(self):
            return "link2"
    
    if name == 'main' :
        applicationLogFileHandler = handlers.TimedRotatingFileHandler('rot_access_file', "midnight", 1)
        applicationLogFileHandler.setLevel(logging.DEBUG)
        applicationLogFileHandler.setFormatter(_cplogging.logfmt)
        cherrypy.log.access_log.addHandler(applicationLogFileHandler)
    
        applicationLogFileHandler = handlers.TimedRotatingFileHandler('rot_error_file', "midnight", 1)
        applicationLogFileHandler.setLevel(logging.DEBUG)
        applicationLogFileHandler.setFormatter(_cplogging.logfmt)
        cherrypy.log.error_log.addHandler(applicationLogFileHandler)
    
        cherrypy.quickstart(HelloWorld())
    

    Hope this helps!