Flask 모든 request 로깅 예제

1 개요[ | ]

Flask 모든 request 로깅 예제
Flask 모든 method, path, data 로깅 예제

2 예제 1[ | ]

  • 환경변수 FLASK_ENV=development
from flask import Flask
from flask import request
application = app = Flask(__name__)

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>', methods=['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'])
def catch_all(path):
    app.logger.debug('[%s][%s][%s]', request.method, path, request.get_data())
    return 'hello'

if __name__ == "__main__":
    app.run(host='0.0.0.0')

3 예제 2[ | ]

from flask import Flask
from flask import request
application = app = Flask(__name__)

@app.after_request
def after_request(response):
    app.logger.error('[%s] %s %s %s %s %s', response.status, request.method, request.scheme, request.remote_addr, request.full_path, request.get_data())
    return response

if __name__ == "__main__":
    app.run(host='0.0.0.0')
# curl localhost:5000
[2021-07-08 17:28:49,946] ERROR in app: [404 NOT FOUND] GET http 172.18.0.1 /? b''
# curl 'localhost:5000/foo/bar?a=1&b=2'
[2021-07-08 17:30:15,794] ERROR in app: [404 NOT FOUND] GET http 172.18.0.1 /foo/bar?a=1&b=2 b''
# curl -X POST 'localhost:5000/foo/bar?a=1&b=2' -d '{"hello":"world"}'
[2021-07-08 17:31:08,106] ERROR in app: [404 NOT FOUND] POST http 172.18.0.1 /foo/bar?a=1&b=2 b'{"hello":"world"}'

4 예제 3[ | ]

from flask import Flask
from flask import request
application = app = Flask(__name__)

@app.after_request
def after_request(response):
    app.logger.error('[%s] %s %s %s %s %s', response.status, request.method, request.scheme, request.remote_addr, request.full_path, request.get_data())
    return response

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>', methods=['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'])
def catch_all(path):
    return 'OK'

if __name__ == "__main__":
    app.run(host='0.0.0.0')

5 같이 보기[ | ]

6 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}