-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_logger.py
58 lines (44 loc) · 1.55 KB
/
request_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import datetime
import time
import colors
from flask import g, request
from rfc3339 import rfc3339
def attach_logger(app):
@app.before_request
def start_timer():
g.start = time.time()
@app.after_request
def log_request(response):
if request.path == "/favicon.ico":
return response
elif request.path.startswith("/static"):
return response
now = time.time()
duration = round(now - g.start, 2)
dt = datetime.datetime.fromtimestamp(now)
timestamp = rfc3339(dt, utc=True)
ip = request.headers.get("X-Forwarded-For", request.remote_addr)
host = request.host.split(":", 1)[0]
args = dict(request.args)
log_params = [
("method", request.method, "blue"),
("path", request.path, "blue"),
("status", response.status_code, "yellow"),
("duration", duration, "green"),
("time", timestamp, "magenta"),
("ip", ip, "red"),
("host", host, "red"),
("params", args, "blue"),
]
if request.json:
log_params.append(("json", request.json, "white"))
request_id = request.headers.get("X-Request-ID")
if request_id:
log_params.append(("request_id", request_id, "yellow"))
parts = []
for name, value, color in log_params:
part = colors.color("{}={}".format(name, value), fg=color)
parts.append(part)
line = " ".join(parts)
app.logger.info(line)
return response