对特定方法/ api / url禁用cherrypy访问日志

问题描述 投票:3回答:1

问题很简单,我们希望CherryPy不记录被调用的特定公开方法/ API的访问日志。

基本上是在调用此API时,URL的查询字符串中有一些非常敏感的参数,如果泄露,将暴露潜在的安全性。自然,这是一个/ GET请求,不幸的是,这是可以传递参数的唯一方法,因为它是从外部服务到此Web服务器的redirect(302)。

如果不记录URL,也将达到目的。

所以,有没有一种方法可以通过API,URL等过滤访问日志中的日志消息?

谢谢您的帮助。

python cherrypy
1个回答
7
投票

cherrypy默认情况下使用Python的标准logging模块,因此您只需添加自定义过滤器即可。此示例将忽略任何以/foo作为路径前缀的GET请求:

import logging

class IgnoreURLFilter(logging.Filter):
    # simple example of log message filtering

    def __init__(self, ignore):
        self.ignore = 'GET /' + ignore

    def filter(self, record):
        return self.ignore not in record.getMessage()

app = cherrypy.tree.mount( YourApplication() )
app.log.access_log.addFilter( IgnoreURLFilter('foo') )
cherrypy.engine.start()
© www.soinside.com 2019 - 2024. All rights reserved.