2022-11-22 09:51:09 +01:00
|
|
|
import json
|
2022-12-27 12:02:23 +01:00
|
|
|
import sys
|
2022-11-22 09:51:09 +01:00
|
|
|
|
2017-12-30 13:06:24 +01:00
|
|
|
from logging import getLogger
|
|
|
|
|
|
|
|
logger = getLogger('toot')
|
|
|
|
|
2022-12-27 12:02:23 +01:00
|
|
|
VERBOSE = "--verbose" in sys.argv
|
|
|
|
|
2017-12-30 13:06:24 +01:00
|
|
|
|
2020-05-11 12:46:27 +02:00
|
|
|
def censor_secrets(headers):
|
|
|
|
def _censor(k, v):
|
|
|
|
if k == "Authorization":
|
|
|
|
return (k, "***CENSORED***")
|
|
|
|
return k, v
|
|
|
|
|
|
|
|
return {_censor(k, v) for k, v in headers.items()}
|
|
|
|
|
|
|
|
|
2022-12-27 12:02:23 +01:00
|
|
|
def truncate(line):
|
|
|
|
if not VERBOSE and len(line) > 100:
|
|
|
|
return line[:100] + "…"
|
|
|
|
|
|
|
|
return line
|
|
|
|
|
|
|
|
|
2017-12-30 13:06:24 +01:00
|
|
|
def log_request(request):
|
|
|
|
logger.debug(">>> \033[32m{} {}\033[0m".format(request.method, request.url))
|
|
|
|
|
|
|
|
if request.headers:
|
2020-05-11 12:46:27 +02:00
|
|
|
headers = censor_secrets(request.headers)
|
|
|
|
logger.debug(">>> HEADERS: \033[33m{}\033[0m".format(headers))
|
2017-12-30 13:06:24 +01:00
|
|
|
|
|
|
|
if request.data:
|
2022-12-27 12:02:23 +01:00
|
|
|
data = truncate(request.data)
|
|
|
|
logger.debug(">>> DATA: \033[33m{}\033[0m".format(data))
|
2017-12-30 13:06:24 +01:00
|
|
|
|
2022-11-22 09:51:09 +01:00
|
|
|
if request.json:
|
2022-12-27 12:02:23 +01:00
|
|
|
data = truncate(json.dumps(request.json))
|
|
|
|
logger.debug(">>> JSON: \033[33m{}\033[0m".format(data))
|
2022-11-22 09:51:09 +01:00
|
|
|
|
2017-12-30 13:06:24 +01:00
|
|
|
if request.files:
|
|
|
|
logger.debug(">>> FILES: \033[33m{}\033[0m".format(request.files))
|
|
|
|
|
|
|
|
if request.params:
|
|
|
|
logger.debug(">>> PARAMS: \033[33m{}\033[0m".format(request.params))
|
|
|
|
|
|
|
|
|
|
|
|
def log_response(response):
|
2022-12-27 12:02:23 +01:00
|
|
|
content = truncate(response.content.decode())
|
|
|
|
|
2017-12-30 13:06:24 +01:00
|
|
|
if response.ok:
|
|
|
|
logger.debug("<<< \033[32m{}\033[0m".format(response))
|
2022-12-27 12:02:23 +01:00
|
|
|
logger.debug("<<< \033[33m{}\033[0m".format(content))
|
2017-12-30 13:06:24 +01:00
|
|
|
else:
|
|
|
|
logger.debug("<<< \033[31m{}\033[0m".format(response))
|
2022-12-27 12:02:23 +01:00
|
|
|
logger.debug("<<< \033[31m{}\033[0m".format(content))
|
2018-01-14 15:34:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def log_debug(*msgs):
|
|
|
|
logger.debug(" ".join(str(m) for m in msgs))
|