Files
dify/api/extensions/ext_request_logging.py
T
+40 5bbf685035 feat: fix i18n missing keys and merge upstream/main (#24615)
Signed-off-by: -LAN- <[email protected]>
Signed-off-by: kenwoodjw <[email protected]>
Signed-off-by: Yongtao Huang <[email protected]>
Signed-off-by: yihong0618 <[email protected]>
Signed-off-by: zhanluxianshen <[email protected]>
Co-authored-by: -LAN- <[email protected]>
Co-authored-by: GuanMu <[email protected]>
Co-authored-by: Davide Delbianco <[email protected]>
Co-authored-by: NeatGuyCoding <[email protected]>
Co-authored-by: kenwoodjw <[email protected]>
Co-authored-by: Yongtao Huang <[email protected]>
Co-authored-by: Yongtao Huang <[email protected]>
Co-authored-by: Qiang Lee <[email protected]>
Co-authored-by: 李强04 <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Asuka Minato <[email protected]>
Co-authored-by: Matri Qi <[email protected]>
Co-authored-by: huayaoyue6 <[email protected]>
Co-authored-by: Bowen Liang <[email protected]>
Co-authored-by: znn <[email protected]>
Co-authored-by: crazywoola <[email protected]>
Co-authored-by: crazywoola <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: yihong <[email protected]>
Co-authored-by: Muke Wang <[email protected]>
Co-authored-by: wangmuke <[email protected]>
Co-authored-by: Wu Tianwei <[email protected]>
Co-authored-by: quicksand <[email protected]>
Co-authored-by: 非法操作 <[email protected]>
Co-authored-by: zxhlyh <[email protected]>
Co-authored-by: Eric Guo <[email protected]>
Co-authored-by: Zhedong Cen <[email protected]>
Co-authored-by: jiangbo721 <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: hjlarry <[email protected]>
Co-authored-by: lxsummer <[email protected]>
Co-authored-by: 湛露先生 <[email protected]>
Co-authored-by: Guangdong Liu <[email protected]>
Co-authored-by: QuantumGhost <[email protected]>
Co-authored-by: Claude <[email protected]>
Co-authored-by: Yessenia-d <[email protected]>
Co-authored-by: huangzhuo1949 <[email protected]>
Co-authored-by: huangzhuo <[email protected]>
Co-authored-by: 17hz <[email protected]>
Co-authored-by: Amy <[email protected]>
Co-authored-by: Joel <[email protected]>
Co-authored-by: Nite Knite <[email protected]>
Co-authored-by: Yeuoly <[email protected]>
Co-authored-by: Petrus Han <[email protected]>
Co-authored-by: iamjoel <[email protected]>
Co-authored-by: Kalo Chin <[email protected]>
Co-authored-by: Ujjwal Maurya <[email protected]>
Co-authored-by: Maries <[email protected]>
2025-08-27 15:07:28 +08:00

74 lines
2.2 KiB
Python

import json
import logging
import flask
import werkzeug.http
from flask import Flask
from flask.signals import request_finished, request_started
from configs import dify_config
logger = logging.getLogger(__name__)
def _is_content_type_json(content_type: str) -> bool:
if not content_type:
return False
content_type_no_option, _ = werkzeug.http.parse_options_header(content_type)
return content_type_no_option.lower() == "application/json"
def _log_request_started(_sender, **_extra):
"""Log the start of a request."""
if not logger.isEnabledFor(logging.DEBUG):
return
request = flask.request
if not (_is_content_type_json(request.content_type) and request.data):
logger.debug("Received Request %s -> %s", request.method, request.path)
return
try:
json_data = json.loads(request.data)
except (TypeError, ValueError):
logger.exception("Failed to parse JSON request")
return
formatted_json = json.dumps(json_data, ensure_ascii=False, indent=2)
logger.debug(
"Received Request %s -> %s, Request Body:\n%s",
request.method,
request.path,
formatted_json,
)
def _log_request_finished(_sender, response, **_extra):
"""Log the end of a request."""
if not logger.isEnabledFor(logging.DEBUG) or response is None:
return
if not _is_content_type_json(response.content_type):
logger.debug("Response %s %s", response.status, response.content_type)
return
response_data = response.get_data(as_text=True)
try:
json_data = json.loads(response_data)
except (TypeError, ValueError):
logger.exception("Failed to parse JSON response")
return
formatted_json = json.dumps(json_data, ensure_ascii=False, indent=2)
logger.debug(
"Response %s %s, Response Body:\n%s",
response.status,
response.content_type,
formatted_json,
)
def init_app(app: Flask):
"""Initialize the request logging extension."""
if not dify_config.ENABLE_REQUEST_LOGGING:
return
request_started.connect(_log_request_started, app)
request_finished.connect(_log_request_finished, app)