[PR #18952] add flask request hook && log request and response content #29040

Closed
opened 2026-02-21 20:44:45 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/18952

State: closed
Merged: No


When a user http request fails, it is not possible to obtain more useful log content from the log.
for example, when the http 400 error occurs:

2025-04-28 03:38:06,486 INFO [_internal.py:97]  127.0.0.1 - - [28/Apr/2025 03:38:06] "POST /console/api/login HTTP/1.1" 400 -

You can only see the request error and cannot know the actual user parameters to further locate the problem

so I modified the flask hook function content based on the existing one, and printed the input and output content for the application/json request

 @dify_app.before_request
    def before_request():
        # add an unique identifier to each request
        RecyclableContextVar.increment_thread_recycles()

        # log request data info
        request_id = get_request_id()
        if request.headers.get("content-type", "").lower() == "application/json":
            logging.info(f"[before request]|request_id: {request_id},"
                         f" method: {request.method}, url: {request.url}, request_data: {request.get_json()}")
        else:
            logging.info(f"[before request]|request_id: {request_id},"
                         f" method: {request.method}, url: {request.url}")

    # add extra `request_id` field for every response data
    @dify_app.after_request
    def add_extra_info(resp):
        obj = resp.get_json()
        if isinstance(obj, dict):
            request_id = get_request_id()
            obj["request_id"] = request_id
            resp.set_data(json.dumps(obj))
            logging.info(f"[finish request]|request_id: {request_id},"
                         f" response: {obj}")
        return resp

After modification, you can see the user request parameters and return content:

2025-04-28 03:38:58,923 INFO [app_factory.py:38] 265127d1fb [before request]|request_id: 265127d1fb, method: POST, url: http://127.0.0.1:5001/console/api/login, request_data: {'email': 'xxxx@sina.cn', 'password': 'abcs123451'}
2025-04-28 03:38:59,237 INFO [app_factory.py:51] 265127d1fb [finish request]|request_id: 265127d1fb, response: {'code': 'email_or_password_mismatch', 'message': 'The email or password is mismatched.', 'status': 400, 'request_id': '265127d1fb'}
2025-04-28 03:38:59,239 INFO [_internal.py:97]  127.0.0.1 - - [28/Apr/2025 03:38:59] "POST /console/api/login HTTP/1.1" 400 -

http post request log

2025-04-28 03:29:16,753 INFO [app_factory.py:38] e7a60bc84a [before request]|request_id: e7a60bc84a, method: POST, url: http://127.0.0.1:5001/console/api/login, request_data: {'email': 'xxxx@sina.cn', 'password': 'abcs123451'}
2025-04-28 03:29:17,161 INFO [app_factory.py:51] e7a60bc84a [finish request]|request_id: e7a60bc84a, response: {'result': 'success', 'data': {'access_token': 'xxxxx', 'refresh_token': 'xxxxxxx'}, 'request_id': 'e7a60bc84a'}

http post request log

2025-04-28 04:33:03,67 INFO [app_factory.py:30] 30a59db87b [before request]|request_id: 30a59db87b, method: GET, url: http://127.0.0.1:5001/console/api/account/profile
2025-04-28 04:33:03,397 INFO [app_factory.py:40] 30a59db87b [finish request]|request_id: 30a59db87b, response: {'id': '36ef6af8-xxxxxxx', 'name': 'gsmini',  'avatar': None, 'avatar_url': None, 'email': 'xxx@sina.cn', 'is_password_set': True, 'interface_language': 'zh-Hans', 'interface_theme': 'light', 'timezone': 'Asia/Shanghai', 'last_login_at': 1745814762, 'last_login_ip': '127.0.0.1', 'is_admin_group': True,  'created_at': 1745395821, 'request_id': '30a59db87b'}
2025-04-28 04:33:03,399 INFO [_internal.py:97]  127.0.0.1 - - [28/Apr/2025 04:33:03] "GET /console/api/account/profile HTTP/1.1" 200 -

http get request log

**Original Pull Request:** https://github.com/langgenius/dify/pull/18952 **State:** closed **Merged:** No --- When a user http request fails, it is not possible to obtain more useful log content from the log. for example, when the http 400 error occurs: ``` 2025-04-28 03:38:06,486 INFO [_internal.py:97] 127.0.0.1 - - [28/Apr/2025 03:38:06] "POST /console/api/login HTTP/1.1" 400 - ``` > You can only see the request error and cannot know the actual user parameters to further locate the problem ### so I modified the flask hook function content based on the existing one, and printed the input and output content for the application/json request ``` @dify_app.before_request def before_request(): # add an unique identifier to each request RecyclableContextVar.increment_thread_recycles() # log request data info request_id = get_request_id() if request.headers.get("content-type", "").lower() == "application/json": logging.info(f"[before request]|request_id: {request_id}," f" method: {request.method}, url: {request.url}, request_data: {request.get_json()}") else: logging.info(f"[before request]|request_id: {request_id}," f" method: {request.method}, url: {request.url}") # add extra `request_id` field for every response data @dify_app.after_request def add_extra_info(resp): obj = resp.get_json() if isinstance(obj, dict): request_id = get_request_id() obj["request_id"] = request_id resp.set_data(json.dumps(obj)) logging.info(f"[finish request]|request_id: {request_id}," f" response: {obj}") return resp ``` ### After modification, you can see the user request parameters and return content: ``` 2025-04-28 03:38:58,923 INFO [app_factory.py:38] 265127d1fb [before request]|request_id: 265127d1fb, method: POST, url: http://127.0.0.1:5001/console/api/login, request_data: {'email': 'xxxx@sina.cn', 'password': 'abcs123451'} 2025-04-28 03:38:59,237 INFO [app_factory.py:51] 265127d1fb [finish request]|request_id: 265127d1fb, response: {'code': 'email_or_password_mismatch', 'message': 'The email or password is mismatched.', 'status': 400, 'request_id': '265127d1fb'} 2025-04-28 03:38:59,239 INFO [_internal.py:97] 127.0.0.1 - - [28/Apr/2025 03:38:59] "POST /console/api/login HTTP/1.1" 400 - ``` > http post request log ``` 2025-04-28 03:29:16,753 INFO [app_factory.py:38] e7a60bc84a [before request]|request_id: e7a60bc84a, method: POST, url: http://127.0.0.1:5001/console/api/login, request_data: {'email': 'xxxx@sina.cn', 'password': 'abcs123451'} 2025-04-28 03:29:17,161 INFO [app_factory.py:51] e7a60bc84a [finish request]|request_id: e7a60bc84a, response: {'result': 'success', 'data': {'access_token': 'xxxxx', 'refresh_token': 'xxxxxxx'}, 'request_id': 'e7a60bc84a'} ``` > http post request log ``` 2025-04-28 04:33:03,67 INFO [app_factory.py:30] 30a59db87b [before request]|request_id: 30a59db87b, method: GET, url: http://127.0.0.1:5001/console/api/account/profile 2025-04-28 04:33:03,397 INFO [app_factory.py:40] 30a59db87b [finish request]|request_id: 30a59db87b, response: {'id': '36ef6af8-xxxxxxx', 'name': 'gsmini', 'avatar': None, 'avatar_url': None, 'email': 'xxx@sina.cn', 'is_password_set': True, 'interface_language': 'zh-Hans', 'interface_theme': 'light', 'timezone': 'Asia/Shanghai', 'last_login_at': 1745814762, 'last_login_ip': '127.0.0.1', 'is_admin_group': True, 'created_at': 1745395821, 'request_id': '30a59db87b'} 2025-04-28 04:33:03,399 INFO [_internal.py:97] 127.0.0.1 - - [28/Apr/2025 04:33:03] "GET /console/api/account/profile HTTP/1.1" 200 - ``` > http get request log
yindo added the pull-request label 2026-02-21 20:44:45 -05:00
yindo closed this issue 2026-02-21 20:44:45 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#29040