Skip to content

Using root logger + flask outside of flask request context throws RuntimeError #61

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

Closed
ex-nerd opened this issue Oct 15, 2020 · 4 comments

Comments

@ex-nerd
Copy link

ex-nerd commented Oct 15, 2020

I've noticed that I get a RuntimeError: Working outside of application context. if I try to log anything after initializing Flask logs but don't yet have a flask request context.

Replication code is here:

app = Flask("my_app")

logging.basicConfig()

json_logging.init_flask(enable_json=True)
json_logging.init_request_instrument(app)
json_logging.config_root_logger()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.info("logging initialized")

This appears to be a bug in json_logging.util.get_correlation_id expecting that "no request" should have a None value but it actually comes back as an "unbound" instance of werkzeug.local.LocalProxy. This fails to match in the check for request is None and results in a call to self.request_adapter.get_correlation_id_in_request_context that triggers the exception. It appears that the unbound LocalProxy resolves to False so checking both request is None or not bool(request) would work around this issue for Flask. I just don't know how that fix would affect other frameworks.

@bobbui bobbui closed this as completed in d9171fa Oct 15, 2020
@bobbui
Copy link
Owner

bobbui commented Oct 15, 2020

thanks for reporting this, i put in a quick fix on 1.2.9

@ex-nerd
Copy link
Author

ex-nerd commented Oct 16, 2020

I updated to 1.2.9 and am still seeing the error. I'm just typing this straight into the python shell:

import logging
import json_logging
from flask import Flask

app = Flask("my_app")

logging.basicConfig()

json_logging.init_flask(enable_json=True)
json_logging.init_request_instrument(app)
json_logging.config_root_logger()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.info("logging initialized")

Which results in:

--- Logging error ---
Traceback (most recent call last):
  File "/path/to/userhome/.pyenv/versions/3.7.8/lib/python3.7/logging/__init__.py", line 1025, in emit
    msg = self.format(record)
  File "/path/to/userhome/.pyenv/versions/3.7.8/lib/python3.7/logging/__init__.py", line 869, in format
    return fmt.format(record)
  File "/path/to/venv/python-3.7.8/lib/python3.7/site-packages/json_logging/__init__.py", line 243, in format
    log_object = self._format_log_object(record, request_util=_request_util)
  File "/path/to/venv/python-3.7.8/lib/python3.7/site-packages/json_logging/__init__.py", line 341, in _format_log_object
    "correlation_id": request_util.get_correlation_id(within_formatter=True),
  File "/path/to/venv/python-3.7.8/lib/python3.7/site-packages/json_logging/util.py", line 147, in get_correlation_id
    correlation_id = self._get_correlation_id_in_request_header(self.request_adapter, request)
  File "/path/to/venv/python-3.7.8/lib/python3.7/site-packages/json_logging/util.py", line 203, in _get_correlation_id_in_request_header
    value = request_adapter.get_http_header(request, header)
  File "/path/to/venv/python-3.7.8/lib/python3.7/site-packages/json_logging/framework/flask/__init__.py", line 81, in get_http_header
    if header_name in request.headers:
  File "/path/to/venv/python-3.7.8/lib/python3.7/site-packages/werkzeug/local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/path/to/venv/python-3.7.8/lib/python3.7/site-packages/werkzeug/local.py", line 306, in _get_current_object
    return self.__local()
  File "/path/to/venv/python-3.7.8/lib/python3.7/site-packages/flask/globals.py", line 38, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.
Call stack:
  File "<stdin>", line 1, in <module>
Message: 'logging initialized'
Arguments: ()

@bobbui
Copy link
Owner

bobbui commented Oct 16, 2020

my bad, fix it again with 1.2.10

bobbui pushed a commit that referenced this issue Oct 16, 2020
@ex-nerd
Copy link
Author

ex-nerd commented Oct 16, 2020

Sweet, that now works. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants