Skip to content

Commit ccf9d3d

Browse files
authored
Fix failures when flask is installed but requests not (#20)
1 parent 7e95875 commit ccf9d3d

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

reconplogger.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
__version__ = "4.18.0"
1616

1717

18-
try:
18+
flask_requests_patch = False
19+
if find_spec("flask") and find_spec("requests"):
1920
# If flask is installed import the request context objects
2021
# If requests is installed patch the calls to add the correlation id
2122
import requests
@@ -31,8 +32,8 @@ def _request_patch(slf, *args, **kwargs):
3132

3233
setattr(requests.sessions.Session, "request_orig", requests.sessions.Session.request)
3334
requests.sessions.Session.request = _request_patch
34-
except ImportError: # pragma: no cover
35-
pass
35+
flask_requests_patch = True
36+
3637

3738
reconplogger_format = "%(asctime)s\t%(levelname)s -- %(filename)s:%(lineno)s -- %(message)s"
3839

@@ -443,7 +444,7 @@ def filter(self, record):
443444
correlation_id = current_correlation_id.get()
444445
if correlation_id is not None:
445446
record.correlation_id = correlation_id
446-
elif find_spec("flask") and has_request_context():
447+
elif flask_requests_patch and has_request_context():
447448
record.correlation_id = g.correlation_id
448449
return True
449450

0 commit comments

Comments
 (0)