From 6a587ac7fc2fe92166149d87001b6d0557708d64 Mon Sep 17 00:00:00 2001 From: forest Date: Tue, 27 Jul 2021 13:56:58 -0500 Subject: [PATCH] trying to do CaptureLogOutputDuringTestsFilter (but no worky yet) --- capsulflask/__init__.py | 31 +++++++++++++++++++++++++------ capsulflask/console.py | 5 ++++- capsulflask/tests_base.py | 11 ++--------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/capsulflask/__init__.py b/capsulflask/__init__.py index 8ee75df..f685bf8 100644 --- a/capsulflask/__init__.py +++ b/capsulflask/__init__.py @@ -96,21 +96,28 @@ def create_app(): app.config['HUB_URL'] = config.get("HUB_URL", app.config['BASE_URL']) + log_filters = { + 'setLogLevelToDebugForHeartbeatRelatedMessages': { + '()': SetLogLevelToDebugForHeartbeatRelatedMessagesFilter, + } + } + if app.config['TESTING'] != False: + log_filters['captureLogOutputDuringTests'] = { + '()': CaptureLogOutputDuringTestsFilter, + } + + logging_dict_config({ 'version': 1, 'formatters': {'default': { 'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s', }}, - 'filters': { - 'setLogLevelToDebugForHeartbeatRelatedMessages': { - '()': SetLogLevelToDebugForHeartbeatRelatedMessagesFilter, - } - }, + 'filters': log_filters, 'handlers': {'wsgi': { 'class': 'logging.StreamHandler', 'stream': 'ext://flask.logging.wsgi_errors_stream', 'formatter': 'default', - 'filters': ['setLogLevelToDebugForHeartbeatRelatedMessages'] + 'filters': list(log_filters.keys()) }}, 'root': { 'level': app.config['LOG_LEVEL'], @@ -270,3 +277,15 @@ class SetLogLevelToDebugForHeartbeatRelatedMessagesFilter(logging.Filter): return False return True + + +class CaptureLogOutputDuringTestsFilter(logging.Filter): + def filter(self, record): + file_object = open('unittest-output.log', 'a') + file_object.write("%s" % record.msg) + for arg in record.args: + file_object.write("%s" % arg) + + file_object.write("\n") + file_object.close() + return True diff --git a/capsulflask/console.py b/capsulflask/console.py index 05bd3d5..3ca1528 100644 --- a/capsulflask/console.py +++ b/capsulflask/console.py @@ -194,7 +194,10 @@ def detail(id): @bp.route("/create", methods=("GET", "POST")) @account_required def create(): - current_app.logger.debug(f"console.create()!") + + raise "console.create()!" + + current_app.logger.error("console.create()!") vm_sizes = get_model().vm_sizes_dict() operating_systems = get_model().operating_systems_dict() diff --git a/capsulflask/tests_base.py b/capsulflask/tests_base.py index f343f7c..60eebb0 100644 --- a/capsulflask/tests_base.py +++ b/capsulflask/tests_base.py @@ -22,17 +22,10 @@ class BaseTestCase(TestCase): return self.app def setUp(self): - self.logs_from_test = StringIO() - self.logs_handler = logging.StreamHandler(self.logs_from_test) - current_app.logger.addHandler(self.logs_handler) + pass def tearDown(self): - #self.app.logger.removeHandler(self.logs_handler) - #logging.getLogger("asdasd1").warning(f"{self.id()} captured output:\n{self.logs_from_test.getvalue()}\n") - - file_object = open('unittest-output.log', 'a') - file_object.write(f"{self.id()} captured output:\n{self.logs_from_test.getvalue()}\n") - file_object.close() + pass def _login(self, user_email): get_model().login(user_email)