【问题标题】:Python Cherrypy Access Log RotationPython Cherrypy 访问日志轮换
【发布时间】:2010-12-08 18:28:19
【问题描述】:

如果我希望 Cherrypy 的访问日志仅达到固定大小,我将如何使用轮换日志文件?

我已经尝试过http://www.cherrypy.org/wiki/Logging,它似乎已经过时,或者缺少信息。

【问题讨论】:

    标签: python cherrypy logging


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      我已经尝试过http://www.cherrypy.org/wiki/Logging,看起来 已过期,或缺少信息。

      尝试添加:

      import logging
      import logging.handlers
      import cherrypy # you might have imported this already
      

      而不是

      log = app.log
      

      可以试试

      log = cherrypy.log
      

      【讨论】:

        【解决方案3】:

        custom log handlers 的 CherryPy 文档显示了这个例子。

        这是我在应用中使用的稍作修改的版本:

        import logging
        from logging import handlers
        
        def setup_logging():
        
            log = cherrypy.log
        
            # Remove the default FileHandlers if present.
            log.error_file = ""
            log.access_file = ""
        
            maxBytes = getattr(log, "rot_maxBytes", 10000000)
            backupCount = getattr(log, "rot_backupCount", 1000)
        
            # Make a new RotatingFileHandler for the error log.
            fname = getattr(log, "rot_error_file", "log\\error.log")
            h = handlers.RotatingFileHandler(fname, 'a', maxBytes, backupCount)
            h.setLevel(logging.DEBUG)
            h.setFormatter(cherrypy._cplogging.logfmt)
            log.error_log.addHandler(h)
        
            # Make a new RotatingFileHandler for the access log.
            fname = getattr(log, "rot_access_file", "log\\access.log")
            h = handlers.RotatingFileHandler(fname, 'a', maxBytes, backupCount)
            h.setLevel(logging.DEBUG)
            h.setFormatter(cherrypy._cplogging.logfmt)
            log.access_log.addHandler(h)
        
        setup_logging()
        

        【讨论】:

          【解决方案4】:

          Cherrypy 使用标准 Python 日志记录模块进行日志记录。您需要将其更改为使用RotatingFileHandler。此处理程序将为您处理所有事情,包括在日志达到设置的最大大小时旋转日志。

          【讨论】:

            猜你喜欢
            • 2015-04-20
            • 2017-09-25
            • 2010-09-26
            • 2016-09-21
            • 1970-01-01
            • 2021-03-29
            • 1970-01-01
            • 2020-07-15
            • 2017-03-08
            相关资源
            最近更新 更多