我正在尝试更改Flask 0.12中的默认格式化程序。这是我的代码
from pomegranate.routes import app
import os
from flask import request
from flask.logging import Formatter, StreamHandler, DEBUG
class RequestFormatter(Formatter):
def format(self, record):
record.url = request.url
record.remote_addr = request.remote_addr
record.request_id = request.headers.get('X-Request-Id', '')
return super().format(record)
formatter = RequestFormatter(
'[%(asctime)s] %(request_id)s %(remote_addr)s %(url)s\n'
'%(levelname)s %(module)s: %(message)s'
)
stream_handler = StreamHandler()
stream_handler.setLevel(DEBUG)
app.logger.addHandler(stream_handler)
for handler in app.logger.handlers:
handler.setFormatter(formatter)
dev = int(os.getenv('FLASK_DEBUG', 0))
if __name__ == '__main__':
host = '0.0.0.0' if dev == 1 else '127.0.0.1'
app.run(host=host, port=8081, debug=True)
但新的格式化程序似乎没有生效。它仍然使用默认格式化程序。出了什么问题?