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']) 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({ logging_dict_config({
'version': 1, 'version': 1,
'formatters': {'default': { 'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s', 'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}}, }},
'filters': { 'filters': log_filters,
'setLogLevelToDebugForHeartbeatRelatedMessages': {
'()': SetLogLevelToDebugForHeartbeatRelatedMessagesFilter,
}
},
'handlers': {'wsgi': { 'handlers': {'wsgi': {
'class': 'logging.StreamHandler', 'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream', 'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default', 'formatter': 'default',
'filters': ['setLogLevelToDebugForHeartbeatRelatedMessages'] 'filters': list(log_filters.keys())
}}, }},
'root': { 'root': {
'level': app.config['LOG_LEVEL'], 'level': app.config['LOG_LEVEL'],
@ -270,3 +277,15 @@ class SetLogLevelToDebugForHeartbeatRelatedMessagesFilter(logging.Filter):
return False return False
return True 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")) @bp.route("/create", methods=("GET", "POST"))
@account_required @account_required
def create(): 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() vm_sizes = get_model().vm_sizes_dict()
operating_systems = get_model().operating_systems_dict() operating_systems = get_model().operating_systems_dict()

View File

@ -22,17 +22,10 @@ class BaseTestCase(TestCase):
return self.app return self.app
def setUp(self): def setUp(self):
self.logs_from_test = StringIO() pass
self.logs_handler = logging.StreamHandler(self.logs_from_test)
current_app.logger.addHandler(self.logs_handler)
def tearDown(self): def tearDown(self):
#self.app.logger.removeHandler(self.logs_handler) pass
#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()
def _login(self, user_email): def _login(self, user_email):
get_model().login(user_email) get_model().login(user_email)