python - 如何让自定义日志记录在 CherryPy 中工作?

标签 python cherrypy

(在我最后修改代码后略微重写,加上目前给出的答案。感谢 Andrew 提供的示例代码,它为我提供了演示自定义错误日志记录工作以及如何破坏它的起点!)

我正在尝试让自定义日志记录在 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)

随着脚本的运行,日志记录如下:

  • 标准访问日志记录到 access1.log(在全局级别定义)和 access.log(在应用级别定义)
  • 错误记录只会转到 error1.log(在全局级别定义)
  • 没有任何内容记录到 *2.log(如预期的那样)

因此,CherryPy 似乎在将错误日志记录转到特定于应用程序的配置方面存在问题。一般来说,这不会让我担心,除了我确实想使用旋转日志文件处理程序,但我不知道如何在全局级别修改日志记录 session ,就像我在应用程序特定级别所做的那样。

谢谢。

最佳答案

为了回答我关于如何为全局日志而不是应用程序级日志设置日志处理程序的问题,以下是更改:

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,但我保留它们是为了确认我正在覆盖全局设置。

关于python - 如何让自定义日志记录在 CherryPy 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21191233/

相关文章:

python - 如何在 Nginx 和 CherryPy 中启用反向代理但阻止直接访问端口 8080?

python - cherrypy 是如何工作的?当并发率低时,与 Tornado 相比,它可以很好地处理请求

python - pandas:二进制编码 pandas 列中的一组值

python - 通过 Flask 应用程序的 Cherrypy 调度获取正确的静态 URL

python - 新手尝试抓取数据并将其分解

python - 使用 `timeit` 的 60MB 字符串测试加入列表会导致 MemoryError

python - 无法从 Cherrypy 将日期时间序列化为 JSON

python - CherryPy,从 matplotlib 或一般情况下加载图像

python - 迭代捕获单个数据帧中的值计数

python - 如何在 CPython 2.7.2 中停用方法缓存?