Skip to content

Commit ed9e3e3

Browse files
committed
changes for flask v3 Workable#58
1 parent b21fe7b commit ed9e3e3

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

flask_log_request_id/request_id.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import uuid
22
import logging as _logging
3-
4-
from flask import request, g, current_app
3+
import flask
4+
from flask import request, g, current_app, has_app_context, has_request_context
55

66
from .parser import auto_parser
77
from .ctx_fetcher import MultiContextRequestIdFetcher, ExecutedOutsideContext
88

9-
109
logger = _logging.getLogger(__name__)
1110

1211

@@ -15,12 +14,19 @@ def flask_ctx_get_request_id():
1514
Get request id from flask's G object
1615
:return: The id or None if not found.
1716
"""
18-
from flask import _app_ctx_stack as stack # We do not support < Flask 0.9
17+
if not (has_request_context() or has_app_context()):
18+
raise ExecutedOutsideContext()
19+
20+
ctx = None
21+
if hasattr(flask, 'globals') and hasattr(flask.globals, 'app_ctx'):
22+
ctx = flask.globals.app_ctx._get_current_object()
23+
elif hasattr(flask, '_app_ctx_stack'):
24+
ctx = flask._app_ctx_stack.top
1925

20-
if stack.top is None:
26+
if ctx is None:
2127
raise ExecutedOutsideContext()
2228

23-
g_object_attr = stack.top.app.config['LOG_REQUEST_ID_G_OBJECT_ATTRIBUTE']
29+
g_object_attr = ctx.app.config['LOG_REQUEST_ID_G_OBJECT_ATTRIBUTE']
2430
return g.get(g_object_attr, None)
2531

2632

0 commit comments

Comments
 (0)