如何删除" - - "从烧瓶的伐木?

时间:2016-03-30 04:06:38

标签: python flask flask-sqlalchemy werkzeug

当我运行烧瓶0.9时,我得到了日志记录:

127.0.0.1 - - [30/Mar/2016 10:08:38] "GET / HTTP/1.1" 200 -
  1. 如何在- -127.0.0.1之间移除[30/Mar/2006 10:08:38]

  2. 如果我想从日志消息中删除响应代码200我该怎么办?

  3. 任何建议将不胜感激!

    正如@alecxe建议的那样,我列出了我的代码段相对日志记录:

    logging.basicConfig(filename='werkzeug.log', level=logging.INFO)
    logger = logging.getLogger('werkzeug')
    logger.setLevel(logging.INFO)
    

1 个答案:

答案 0 :(得分:9)

您可以继承werkzeug.serving.WSGIRequestHandler来覆盖您不喜欢的行为:

import logging
from flask import Flask
from werkzeug.serving import WSGIRequestHandler, _log

app = Flask(__name__)

@app.route('/hello')
def hello():
    return '<html><body><p>Hello, World.</p></body></html>'

class MyRequestHandler(WSGIRequestHandler):
    # Just like WSGIRequestHandler, but without "- -"
    def log(self, type, message, *args):
        _log(type, '%s [%s] %s\n' % (self.address_string(),
                                         self.log_date_time_string(),
                                         message % args))

    # Just like WSGIRequestHandler, but without "code"
    def log_request(self, code='-', size='-'):
        self.log('info', '"%s" %s', self.requestline, size)

if __name__=="__main__":
    logging.basicConfig(filename='werkzeug.log', level=logging.INFO)
    logger = logging.getLogger('werkzeug')
    logger.setLevel(logging.INFO)
    app.run(debug=True, request_handler=MyRequestHandler)

生成的日志文件:

INFO:werkzeug: * Running on http://127.0.0.1:5000/
INFO:werkzeug: * Restarting with reloader
INFO:werkzeug:127.0.0.1 [30/Mar/2016 02:28:24] "GET /?foo HTTP/1.1" -
INFO:werkzeug:127.0.0.1 [30/Mar/2016 02:28:28] "GET /hello HTTP/1.1" -