Description
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.