0

我想为cherrypy中的访问和错误创建单独的日志文件。我阅读了http://docs.cherrypy.org/dev/refman/_cplogging.html,但我没有编写代码来实现日志记录。

我阅读了更多链接Python Cherrypy Access Log Rotation

请提供代码和文档如何在cherrypy中实现日志记录请通过提供示例代码帮助我

我有一个 hello.py 文件

进口樱桃

类 HelloWorld: def index(self): return "Hello world!" index.exposed = 真

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

    #print self.j
    return "link"

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

如果名称== '':cherrypy.quickstart(HelloWorld())

实现日志记录需要哪些更改,请提供代码..

4

1 回答 1

0

尝试这个...

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

希望这可以帮助!

于 2013-09-23T12:52:31.883 回答