From f2d6d967667114cb9533384de56bb8e393af67a7 Mon Sep 17 00:00:00 2001 From: Georgi Vachkov Date: Thu, 18 Apr 2019 12:18:03 +0300 Subject: [PATCH] Fix logging not usable outside request --- sap/cf_logging/falcon_logging/context.py | 3 ++- sap/cf_logging/flask_logging/context.py | 5 +++-- test-requirements.txt | 2 +- tests/test_falcon_logging.py | 10 ++++++++++ tests/test_flask_logging.py | 9 +++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/sap/cf_logging/falcon_logging/context.py b/sap/cf_logging/falcon_logging/context.py index 046ee4e..317385b 100644 --- a/sap/cf_logging/falcon_logging/context.py +++ b/sap/cf_logging/falcon_logging/context.py @@ -11,7 +11,8 @@ class FalconContext(Context): """ Stores logging context in Falcon's request object""" def set(self, key, value, request): - request.context[key] = value + if request: + request.context[key] = value def get(self, key, request): return request.context.get(key) if request else None diff --git a/sap/cf_logging/flask_logging/context.py b/sap/cf_logging/flask_logging/context.py index 0e3dd71..cd74ff9 100644 --- a/sap/cf_logging/flask_logging/context.py +++ b/sap/cf_logging/flask_logging/context.py @@ -11,7 +11,8 @@ class FlaskContext(Context): """ Stores logging context in Flask's request scope """ def set(self, key, value, request): - setattr(g, key, value) + if g: + setattr(g, key, value) def get(self, key, request): - return getattr(g, key, None) + return getattr(g, key, None) if g else None diff --git a/test-requirements.txt b/test-requirements.txt index 4de55c8..bdfbb1e 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -10,4 +10,4 @@ pytest-cov==2.5.1 pytest-mock==1.6.3 pylint==1.9.2 tox -django +django < 2.2 diff --git a/tests/test_falcon_logging.py b/tests/test_falcon_logging.py index 53d56d3..8c252b0 100644 --- a/tests/test_falcon_logging.py +++ b/tests/test_falcon_logging.py @@ -90,6 +90,16 @@ def test_web_log(): """ That the custom properties are logged """ _user_logging({}, {'myprop': 'myval'}, {'myprop': v_str('myval')}) +def test_logging_without_request(): + """ Test logger is usable in non request context """ + app = falcon.API() + _set_up_falcon_logging(app) + cf_logging.FRAMEWORK.context.set_correlation_id('value') + + logger, stream = config_logger('main.logger') + logger.info('works') + assert check_log_record(stream, JOB_LOG_SCHEMA, {'msg': v_str('works')}) == {} + def test_correlation_id(): """ Test the correlation id is logged when coming from the headers """ diff --git a/tests/test_flask_logging.py b/tests/test_flask_logging.py index ac60953..49a9754 100644 --- a/tests/test_flask_logging.py +++ b/tests/test_flask_logging.py @@ -66,6 +66,15 @@ def test_correlation_id(): {'correlation_id': v_str('298ebf9d-be1d-11e7-88ff-2c44fd152860')}) +def test_logging_without_request(): + """ Test logger is usable in non request context """ + app = Flask(__name__) + _set_up_flask_logging(app) + logger, stream = config_logger('main.logger') + logger.info('works') + assert check_log_record(stream, JOB_LOG_SCHEMA, {'msg': v_str('works')}) == {} + + # Helper functions def _set_up_flask_logging(app, level=logging.DEBUG): cf_logging._SETUP_DONE = False