2014-01-17 78 views
1

(在我的结尾处加上代码修改以及迄今为止给出的答案后略有改写。感谢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在将错误日志记录到应用程序特定的配置中时遇到了问题。一般来说,除了我想要使用循环日志文件处理程序,我不知道如何在全局级别修改日志记录会话,就像我为应用程序特定的级别所做的那样,这并不会让我担心。

谢谢。

+0

它适合我。 CherryPy的错误是否会通过? – jwalker

+0

是的,他们这样做。当发生异常时,我会得到堆栈跟踪以及更详细的标题值列表。 –

+0

用测试示例更新了我的答案。 –

回答

0

回答我有关如何设置日志处理程序,为全球数,而不是应用程序级别的日志问题,这里的变化:

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,但我离开他们这样确认我覆盖了全局设置。

0

使用这样的事情...

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需要被转动。

希望这有助于和快乐编码!

+0

本质上它与问题 – jwalker

+0

中的调用相同,这对我有效。他可能遇到的另一个问题是,运行他的cherrypy应用程序的用户没有写入error.log文件的权限。 –

+0

是的,因此我的问题 – jwalker

相关问题