Created
August 19, 2017 09:43
-
-
Save behrtam/03b484db328bd1e4c95b201871fe6c15 to your computer and use it in GitHub Desktop.
Flask Injecting Request Information – still uses old format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from flask import Flask, request | |
from flask.logging import default_handler | |
class RequestFormatter(logging.Formatter): | |
def format(self, record): | |
record.url = request.url | |
record.remote_addr = request.remote_addr | |
return super().format(record) | |
app = Flask(__name__) | |
formatter = RequestFormatter( | |
'[%(asctime)s] %(remote_addr)s requested %(url)s\n' | |
'%(levelname)s in %(module)s: %(message)s' | |
) | |
default_handler.setFormatter(formatter) | |
@app.route("/") | |
def hello(): | |
return "Hello World!" | |
if __name__ == "__main__": | |
app.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(venv) $ python ./app.py | |
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) | |
127.0.0.1 - - [19/Aug/2017 11:38:08] "GET / HTTP/1.1" 200 - | |
127.0.0.1 - - [19/Aug/2017 11:41:23] "POST / HTTP/1.1" 405 - | |
^C | |
(venv) $ python | |
Python 3.6.2 (default, Jul 17 2017, 16:44:45) | |
>>> import flask | |
>>> flask.__version__ | |
'0.13-dev' | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment