Python 如何让定制日志在CherryPy中工作?
(在我最后的代码修改之后,再加上到目前为止给出的答案,我稍微重写了一下。感谢Andrew提供的示例代码,它为我演示自定义错误日志工作以及如何打破它提供了起点!) 我正在尝试让自定义日志在CherryPy中工作。我还希望有日志文件旋转,所以我已经按照文档中的说明替换了日志处理程序 脚本_dir在前面的代码中设置为脚本运行的目录Python 如何让定制日志在CherryPy中工作?,python,cherrypy,Python,Cherrypy,(在我最后的代码修改之后,再加上到目前为止给出的答案,我稍微重写了一下。感谢Andrew提供的示例代码,它为我演示自定义错误日志工作以及如何打破它提供了起点!) 我正在尝试让自定义日志在CherryPy中工作。我还希望有日志文件旋转,所以我已经按照文档中的说明替换了日志处理程序 脚本_dir在前面的代码中设置为脚本运行的目录 cherrypy.config.update({'server.socket_host': '0.0.0.0', 'serv
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)
脚本运行时,日志记录如下所示:
- 标准访问日志同时指向access1.log(在全局级别定义)和access.log(在应用程序级别定义)
- 错误日志记录仅转到error1.log(在全局级别定义)
- 未将任何内容记录到*2.log(如预期的那样)
谢谢。用这样的东西
import cherrypy
from cherrypy import log
class MyApp(object):
def index(self):
log.error(msg='This is My Error ', context='HTTP', severity=20, traceback=True)
return "Hello World!"
index.exposed = True
cherrypy.tree.mount(MyApp(), "/")
cherrypy.config.update({'tools.staticdir.on': True,
'tools.staticdir.dir': 'C:\\Documents and Settings\\d\\My Documents\\Aptana Studio 3 Workspace\\ScratchPad',
'log.access_file' : "access.log",
'log.error_file' : "error.log",
'log.screen' : False,
'tools.sessions.on': True,
})
cherrypy.engine.start()
cherrypy.engine.block()
这将记录错误。我相信在代码中加载配置时会出现问题。我还认为静态dirs需要被转换
希望这有助于和快乐的编码 要回答我关于如何为全局日志而不是应用程序级日志设置日志处理程序的问题,以下是更改:
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': True,
'tools.sessions.on': True,
})
config = {'/':
{
}
}
application = cherrypy.tree.mount(HealthCheck(script_dir, service_fqdn, my_ip), "/", config)
logscope = cherrypy.log
# Make a new RotatingFileHandler for the error log.
fname = getattr(logscope, "rot_error_file", "error.log")
h = handlers.TimedRotatingFileHandler(fname, when='midnight')
h.setLevel(logging.DEBUG)
h.setFormatter(_cplogging.logfmt)
logscope.error_file = ""
logscope.error_log.addHandler(h)
# Make a new RotatingFileHandler for the access log.
fname = getattr(logscope, "rot_access_file", "access.log")
h = handlers.TimedRotatingFileHandler(fname, when='midnight')
h.setLevel(logging.DEBUG)
h.setFormatter(_cplogging.logfmt)
logscope.access_file = ""
logscope.access_log.addHandler(h)
或者,简而言之:
- 将应用程序配置保留为空。我只是想让CherryPy安静地开始
- 将日志范围从application.log更改为cherrypy.log
为了更整洁一点,对access1.log和error1.log的引用可以更改为access.log和error.log,但我这样做是为了确认我覆盖了全局设置。对我来说效果不错。CherryPy的错误会通过吗?是的,会。当出现异常时,我会得到堆栈跟踪以及更详细的标题值列表。使用测试示例更新了我的答案。有趣的是cherrypy似乎需要抛出一个错误,以便将错误记录到日志文件中。我会进一步调查的。再说一遍,你到底什么时候想记录一些东西?从你的例子中,我可以看出你的一切都是静态的。另外,您是否尝试将您的处理程序全局附加到cherrypy.log?本质上,这与问题中的调用相同,这对我有效。他可能遇到的另一个问题是,运行cherrypy应用程序的用户没有写入error.log文件的权限。是的,因此我的问题我认为这不是写入权限问题。“cherrypy.log.error”与“cherrypy.log”相同吗?我刚刚意识到,我的呼叫可能试图写入访问日志,但必须遵循严格的格式?否-切换到cherrypy.log.error没有任何区别:-(