diff --git a/flask_log_request_id/request_id.py b/flask_log_request_id/request_id.py index 9f38ea0..9cf0a6a 100644 --- a/flask_log_request_id/request_id.py +++ b/flask_log_request_id/request_id.py @@ -1,6 +1,7 @@ import uuid import logging as _logging +import flask from flask import request, g, current_app from .parser import auto_parser @@ -15,12 +16,19 @@ def flask_ctx_get_request_id(): Get request id from flask's G object :return: The id or None if not found. """ - from flask import _app_ctx_stack as stack # We do not support < Flask 0.9 - - if stack.top is None: + # from flask import _app_ctx_stack as stack # We do not support < Flask 0.9 + if hasattr(flask, 'globals') and \ + hasattr(flask.globals, 'request_ctx'): + # update session for Flask >= 2.2 + ctx = flask.globals.request_ctx._get_current_object() + else: # pragma: no cover + # update session for Flask < 2.2 + ctx = flask._request_ctx_stack.top + + if ctx is None: raise ExecutedOutsideContext() - g_object_attr = stack.top.app.config['LOG_REQUEST_ID_G_OBJECT_ATTRIBUTE'] + g_object_attr = ctx.app.config['LOG_REQUEST_ID_G_OBJECT_ATTRIBUTE'] return g.get(g_object_attr, None)