python – CherryPy日志记录:如何配置和使用全局和应用程序级别记录器?

python – CherryPy日志记录:如何配置和使用全局和应用程序级别记录器?,第1张

概述我在登录方面遇到了麻烦.我正在运行CherryPy 3.2并且我一直在阅读文档 here,但是没有找到任何关于如何为输出配置本地日志文件以及如何写入它的示例. Raspberry.py: import socketimport sysimport cherrypyapp_roots = { # Sean's laptop dev environment. 我在登录方面遇到了麻烦.我正在运行CherryPy 3.2并且我一直在阅读文档 here,但是没有找到任何关于如何为输出配置本地日志文件以及如何写入它的示例.

RaspBerry.py:

import socketimport sysimport cherrypyapp_roots = {                # Sean's laptop dev environment.                "mylaptop": "/home/src/local-mydomain.com/py",# Hosted dev environment.                  "mydomain.com" : "/home/dev/src/py"            }hostname = socket.gethostname()CherryPyLog = cherrypy.tree.mount().logif hostname not in app_roots:    CherryPyLog("The following hostname does not have an app_root entry in raspBerry.py.  Exiting early.")    sys.exit()sys.stdout = sys.stderrsys.path.append(app_roots[hostname])import osos.chdir(app_root)# Setup for raspBerry application logging.import datetimetoday = datetime.datetime.today()log.access_file = "{0}/{1}.raspBerry.access.log".format(app_roots[hostname],today.strftime("%Y%m%d-%H%M"))log.error_file = "{0}/{1}.raspBerry.error.log".format(app_roots[hostname],today.strftime("%Y%m%d-%H%M"))#Testing loggerlog("{0} -- Logger configured".format(today.strftime("%Y%m%d-%H%M%s")))import atexitcherrypy.config.update({'environment': 'embedded'})if cherrypy.__version__.startswith('3.0') and cherrypy.engine.state == 0:    cherrypy.engine.start(blocking = False)    atexit.register(cherrypy.engine.stop)from web.controllers.root import RaspBerryRequestHandlerapplication = cherrypy.Application(RaspBerryRequestHandler(),script_name = None,config = None)

更新:这是我最终使用的代码块.

app_roots = {                # Sean's laptop dev environment.                "mylaptop": "/home/src/local-plottools.com/py",# Hosted dev environment.                  "myDomain" : "/home/dev/src/py"            }import sockethostname = socket.gethostname()import cherrypyimport sysif hostname not in app_roots:    cherrypy.log("The hostname {0} does not have an app_root entry in {1}.  Exiting early.".format(hostname,__file__))    sys.exit()sys.stdout = sys.stderrsys.path.append(app_roots[hostname])import osos.chdir(app_roots[hostname])from web.controllers.root import RaspBerryRequestHandlercherrypy.config.update({    'log.access_file': "{0}/cherrypy-access.log".format(app_roots[hostname]),'log.error_file': "{0}/cherrypy.log".format(app_roots[hostname]),"server.thread_pool" : 10})# special case,handling deBUG sessions when quickstart is needed.if __name__ == "__main__":    cherrypy.config.update({                                'log.screen': True,"server.socket_port": 8000                            })    cherrypy.quickstart(RaspBerryRequestHandler())    sys.exit()# This configuration is needed for running under mod_wsgi.  See here:  http://tools.cherrypy.org/wiki/MoDWsgi    cherrypy.config.update({'environment': 'embedded'})applicationLogname = "{0}/raspBerry.log".format(app_roots[hostname])from logging import handlersapplicationLogfileHandler = handlers.RotatingfileHandler(applicationLogname,'a',10000000,1000)import loggingapplicationLogfileHandler.setLevel(logging.DEBUG)from cherrypy import _cploggingapplicationLogfileHandler.setFormatter(_cplogging.logfmt)cherrypy.log.error_log.addHandler(applicationLogfileHandler)application = cherrypy.Application(RaspBerryRequestHandler(),None)
解决方法 简化一下:
import osimport socketimport sysimport cherrypyapp_roots = {                # Sean's laptop dev environment.                "mylaptop": "/home/src/local-mydomain.com/py",# Hosted dev environment.                  "mydomain.com" : "/home/dev/src/py"            }hostname = socket.gethostname()if hostname not in app_roots:    cherrypy.log("The hostname %r does not have an app_root entry in "                 "raspBerry.py.  Exiting early." % hostname)    sys.exit()sys.path.append(app_roots[hostname])os.chdir(app_root)cherrypy.config.update({    'environment': 'embedded','log.access_file': "{0}/raspBerry.access.log".format(app_roots[hostname]),'log.error_file': "{0}/raspBerry.error.log".format(app_roots[hostname]),})from web.controllers.root import RaspBerryRequestHandlerapplication = cherrypy.tree.mount(RaspBerryRequestHandler(),'/')# Insert log changes herecherrypy.engine.start()

如果您每天需要不同的日志,请使用RotatingfileHandler,如http://www.cherrypy.org/wiki/Logging#CustomHandlers所述.我认为您缺少的重要一点是,只有在您实例化应用程序后才应该使用app.log(例如,通过tree.mount()),如上所述),但在engine.start之前.也就是说,对于错误日志:

application = cherrypy.tree.mount(RaspBerryRequestHandler(),'/')log = application.loglog.error_file = ""# Make a new RotatingfileHandler for the error log.fname = "{0}/raspBerry.error.log".format(app_roots[hostname])h = handlers.RotatingfileHandler(fname,1000)h.setLevel(DEBUG)h.setFormatter(_cplogging.logfmt)log.error_log.addHandler(h)cherrypy.engine.start()

希望有帮助……

总结

以上是内存溢出为你收集整理的python – CherryPy日志记录:如何配置和使用全局和应用程序级别记录器?全部内容,希望文章能够帮你解决python – CherryPy日志记录:如何配置和使用全局和应用程序级别记录器?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1206760.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存