Skip to content

Instantly share code, notes, and snippets.

@behrtam
Created August 19, 2017 09:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behrtam/03b484db328bd1e4c95b201871fe6c15 to your computer and use it in GitHub Desktop.
Save behrtam/03b484db328bd1e4c95b201871fe6c15 to your computer and use it in GitHub Desktop.
Flask Injecting Request Information – still uses old format
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()
(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