(在我的结尾处加上代码修改以及迄今为止给出的答案后略有改写。感谢Andrew为示例代码给出了展示自定义错误日志记录起作用的起点,然后我如何打破它!)如何在CherryPy中使用自定义日志记录工作?
我试图在CherryPy中使用自定义日志记录工作。我也想要有日志文件轮换,所以我按照文档中的说明来替换日志处理程序。
script_dir在我的代码之前设置到运行脚本的目录。
cherrypy.config.update({'server.socket_host': '0.0.0.0',
'server.socket_port': 1234,
'tools.staticdir.on': True,
'tools.staticdir.dir': script_dir,
'log.access_file': "access1.log",
'log.error_file': "error1.log",
'log.screen': False,
'tools.sessions.on': True,
})
config = {'/':
{
'tools.staticdir.on': True,
'tools.staticdir.dir': script_dir,
'log.access_file': "access2.log",
'log.error_file': "error2.log",
'log.screen': False,
'tools.sessions.on': True,
}
}
application = cherrypy.tree.mount(MyApp(), "/", config)
log = application.log
# Make a new RotatingFileHandler for the error log.
fname = getattr(log, "rot_error_file", "error.log")
h = handlers.TimedRotatingFileHandler(fname, when='midnight')
h.setLevel(logging.DEBUG)
h.setFormatter(_cplogging.logfmt)
log.error_file = ""
log.error_log.addHandler(h)
# Make a new RotatingFileHandler for the access log.
fname = getattr(log, "rot_access_file", "access.log")
h = handlers.TimedRotatingFileHandler(fname, when='midnight')
h.setLevel(logging.DEBUG)
h.setFormatter(_cplogging.logfmt)
log.access_file = ""
log.access_log.addHandler(h)
随着脚本运行,日志记录情况如下:
- 标准访问记录日志转到BOTH access1.log(在全球范围内定义)和access.log里(在应用程序级别定义)
- 错误日志记录只到error1.log(在全球范围内所定义的)
- 没有被记录到* 2.登录(如预期)
因此,看起来CherryPy在将错误日志记录到应用程序特定的配置中时遇到了问题。一般来说,除了我想要使用循环日志文件处理程序,我不知道如何在全局级别修改日志记录会话,就像我为应用程序特定的级别所做的那样,这并不会让我担心。
谢谢。
它适合我。 CherryPy的错误是否会通过? – jwalker
是的,他们这样做。当发生异常时,我会得到堆栈跟踪以及更详细的标题值列表。 –
用测试示例更新了我的答案。 –