沉默特定方法/ api/url的cherrypy访问日志

use*_*937 3 python cherrypy

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

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

如果它不记录URL,那也可以达到目的.

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

在此先感谢您的帮助.

Mat*_*vor 6

cherrypy logging默认使用Python的标准模块,因此您只需添加自定义过滤器即可.此示例将忽略任何带有/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 not self.ignore in record.getMessage()

app = cherrypy.tree.mount( YourApplication() )
app.log.access_log.addFilter( IgnoreURLFilter('foo') )
cherrypy.engine.start()
Run Code Online (Sandbox Code Playgroud)