Skip to content

Changes to support flask v3 #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions flask_log_request_id/request_id.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import uuid
import logging as _logging

from flask import request, g, current_app
import flask
from flask import request, g, current_app, has_app_context, has_request_context

from .parser import auto_parser
from .ctx_fetcher import MultiContextRequestIdFetcher, ExecutedOutsideContext


logger = _logging.getLogger(__name__)


Expand All @@ -15,12 +14,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 not (has_request_context() or has_app_context()):
raise ExecutedOutsideContext()

ctx = None
if hasattr(flask, 'globals') and hasattr(flask.globals, 'app_ctx'):
ctx = flask.globals.app_ctx._get_current_object()
elif hasattr(flask, '_app_ctx_stack'):
ctx = flask._app_ctx_stack.top

if stack.top is None:
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)


Expand Down