烧瓶阻止特定端点记录

时间:2020-05-25 10:52:54

标签: python flask logging

我的烧瓶应用程序中有一些带有bokeh的ajax调用,为了获取其数据,bokeh绘图每秒向某个端点执行POST请求,现在我不断得到如下输出:

127.0.0.1 - - [25/May/2020 12:50:17] "POST /statusdata/ HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2020 12:50:17] "POST /statusdata/ HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2020 12:50:19] "POST /statusdata/ HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2020 12:50:19] "POST /statusdata/ HTTP/1.1" 200 -

如何从flask的调试日志中排除路由/ statusdata /? 感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以向记录器添加自定义过滤器,以查找路由名称。如果您只是在使用开发服务器,则它将是werkzeug处理程序。当您进入生产阶段时,必须找到特定的处理程序。一个完整的示例(尽管服务器上未显示任何记录,您也可以在浏览器控制台中监视该表单以确认表单已提交):

from flask import Flask, render_template_string
import logging


class AjaxFilter(logging.Filter):
    def filter(self, record):  
        return "statusdata" not in record.getMessage()

log = logging.getLogger('werkzeug')
log.addFilter(AjaxFilter())


home_template = """
<form method="post" action="{{ url_for('route_a') }}" class="testAjax">
<input type="submit" value="submit A">
</form>

<form method="post" action="{{ url_for('statusdata') }}" class="testAjax">
<input type="submit" value="submit B">
</form>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
   $(".testAjax").submit(function(e) {
       e.preventDefault();
       var form = $(this);
       var url = form.attr('action');

       $.ajax({
           type: "POST",
           url: url,
           data: form.serialize(),
           context: form,
           success: function(resp) {
               console.log(resp);   
           }
       });
   });
</script>
"""

app = Flask('__name__')


@app.route('/')
def home():
    return render_template_string(home_template)

@app.route('/a', methods=['POST'])
def route_a():
    return "1"

@app.route('/statusdata/', methods=['POST'])
def statusdata():
    return "2"

if __name__ == '__main__':
    app.run()