trying to do CaptureLogOutputDuringTestsFilter (but no worky yet)

This commit is contained in:
forest 2021-07-27 13:56:58 -05:00 committed by 3wc
parent 4e510ebb01
commit 6a587ac7fc
3 changed files with 31 additions and 16 deletions

View File

@ -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

View File

@ -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()

View File

@ -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)