From 137b45c12c500ceb5f4e1430099b19abf153fda3 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Wed, 29 Nov 2017 06:45:09 +0100 Subject: [PATCH 1/4] Enable logging custom modules --- dynamicweb/settings/base.py | 70 ++++++++++++++++++++++++++++++------- opennebula_api/models.py | 14 ++++---- 2 files changed, 65 insertions(+), 19 deletions(-) diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 19c263d5..265e97a0 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -575,25 +575,69 @@ if 'info@ungleich.ch' not in DCL_ERROR_EMAILS_TO_LIST: ENABLE_DEBUG_LOGGING = bool_env('ENABLE_DEBUG_LOGGING') -if ENABLE_DEBUG_LOGGING: - LOGGING = { - 'version': 1, - 'disable_existing_loggers': False, - 'handlers': { +loggers_dict = { + 'django': { + 'handlers': ['file'], + 'level': 'DEBUG', + 'propagate': True, + }, + } +handlers_dict = { 'file': { 'level': 'DEBUG', 'class': 'logging.FileHandler', 'filename': "{PROJECT_DIR}/debug.log".format( PROJECT_DIR=PROJECT_DIR), }, - }, - 'loggers': { - 'django': { - 'handlers': ['file'], - 'level': 'DEBUG', - 'propagate': True, - }, - }, +} + +MODULES_TO_LOG = env('MODULES_TO_LOG') +MODULES_TO_LOG_LEVEL = env('MODULES_TO_LOG_LEVEL') + +if MODULES_TO_LOG_LEVEL is None: + MODULES_TO_LOG_LEVEL = 'DEBUG' + +if MODULES_TO_LOG: + if ',' in MODULES_TO_LOG: + modules_to_log_list = MODULES_TO_LOG.split(',') + for custom_module in modules_to_log_list: + logger_item = { + custom_module: { + 'handlers': ['custom_file'], + 'level': MODULES_TO_LOG_LEVEL, + 'propagate': True + } + } + loggers_dict.update(logger_item) + else: + logger_item = { + MODULES_TO_LOG: { + 'handlers': ['custom_file'], + 'level': MODULES_TO_LOG_LEVEL, + 'propagate': True + } + } + loggers_dict.update(logger_item) + + custom_handler_item = { + 'custom_file': { + 'level': MODULES_TO_LOG_LEVEL, + 'class': 'logging.FileHandler', + 'filename': + "{PROJECT_DIR}/custom_{LEVEL}.log".format( + LEVEL=MODULES_TO_LOG_LEVEL.lower(), + PROJECT_DIR=PROJECT_DIR + ) + } + } + handlers_dict.update(custom_handler_item) + +if ENABLE_DEBUG_LOGGING: + LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'handlers': handlers_dict, + 'loggers': loggers_dict } TEST_MANAGE_SSH_KEY_PUBKEY = env('TEST_MANAGE_SSH_KEY_PUBKEY') diff --git a/opennebula_api/models.py b/opennebula_api/models.py index 33812c52..d9b0b6c2 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -301,12 +301,14 @@ class OpenNebulaManager(): """ - vm_id = self.client.call(oca.VmTemplate.METHODS['instantiate'], - template.id, - '', - True, - vm_specs, - False) + try: + vm_id = self.client.call( + oca.VmTemplate.METHODS['instantiate'], template.id, '', True, + vm_specs, False + ) + except OpenNebulaException as err: + logger.error("OpenNebulaException: {0}".format(err)) + return None self.oneadmin_client.call( oca.VirtualMachine.METHODS['action'], From 6e82add9351b999024c1c2f544aa9c1a4b7cece8 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 29 Nov 2017 07:07:03 +0100 Subject: [PATCH 2/4] Rename debug.log to django-debug.log and refactor MODULES_TO_LOG_LEVEL --- dynamicweb/settings/base.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 265e97a0..10cdf40c 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -586,16 +586,16 @@ handlers_dict = { 'file': { 'level': 'DEBUG', 'class': 'logging.FileHandler', - 'filename': "{PROJECT_DIR}/debug.log".format( + 'filename': "{PROJECT_DIR}/django-debug.log".format( PROJECT_DIR=PROJECT_DIR), }, } MODULES_TO_LOG = env('MODULES_TO_LOG') -MODULES_TO_LOG_LEVEL = env('MODULES_TO_LOG_LEVEL') +LOG_LEVEL = env('LOG_LEVEL') -if MODULES_TO_LOG_LEVEL is None: - MODULES_TO_LOG_LEVEL = 'DEBUG' +if LOG_LEVEL is None: + LOG_LEVEL = 'DEBUG' if MODULES_TO_LOG: if ',' in MODULES_TO_LOG: @@ -604,7 +604,7 @@ if MODULES_TO_LOG: logger_item = { custom_module: { 'handlers': ['custom_file'], - 'level': MODULES_TO_LOG_LEVEL, + 'level': LOG_LEVEL, 'propagate': True } } @@ -613,7 +613,7 @@ if MODULES_TO_LOG: logger_item = { MODULES_TO_LOG: { 'handlers': ['custom_file'], - 'level': MODULES_TO_LOG_LEVEL, + 'level': LOG_LEVEL, 'propagate': True } } @@ -621,11 +621,11 @@ if MODULES_TO_LOG: custom_handler_item = { 'custom_file': { - 'level': MODULES_TO_LOG_LEVEL, + 'level': LOG_LEVEL, 'class': 'logging.FileHandler', 'filename': "{PROJECT_DIR}/custom_{LEVEL}.log".format( - LEVEL=MODULES_TO_LOG_LEVEL.lower(), + LEVEL=LOG_LEVEL.lower(), PROJECT_DIR=PROJECT_DIR ) } From b6e734d84e42c20b6469cde69f7ab8fcd6f4ac84 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 29 Nov 2017 07:32:17 +0100 Subject: [PATCH 3/4] Refactor code --- dynamicweb/settings/base.py | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 10cdf40c..3f40d8a7 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -573,31 +573,19 @@ if DCL_ERROR_EMAILS_TO is not None: if 'info@ungleich.ch' not in DCL_ERROR_EMAILS_TO_LIST: DCL_ERROR_EMAILS_TO_LIST.append('info@ungleich.ch') -ENABLE_DEBUG_LOGGING = bool_env('ENABLE_DEBUG_LOGGING') - -loggers_dict = { - 'django': { - 'handlers': ['file'], - 'level': 'DEBUG', - 'propagate': True, - }, - } -handlers_dict = { - 'file': { - 'level': 'DEBUG', - 'class': 'logging.FileHandler', - 'filename': "{PROJECT_DIR}/django-debug.log".format( - PROJECT_DIR=PROJECT_DIR), - }, -} - +ENABLE_LOGGING = bool_env('ENABLE_LOGGING') MODULES_TO_LOG = env('MODULES_TO_LOG') LOG_LEVEL = env('LOG_LEVEL') if LOG_LEVEL is None: LOG_LEVEL = 'DEBUG' -if MODULES_TO_LOG: +if ENABLE_LOGGING: + loggers_dict = {} + handlers_dict = {} + if MODULES_TO_LOG is None: + # set MODULES_TO_LOG to django, if it is not set + MODULES_TO_LOG = 'django' if ',' in MODULES_TO_LOG: modules_to_log_list = MODULES_TO_LOG.split(',') for custom_module in modules_to_log_list: @@ -624,15 +612,13 @@ if MODULES_TO_LOG: 'level': LOG_LEVEL, 'class': 'logging.FileHandler', 'filename': - "{PROJECT_DIR}/custom_{LEVEL}.log".format( + "{PROJECT_DIR}/{LEVEL}.log".format( LEVEL=LOG_LEVEL.lower(), PROJECT_DIR=PROJECT_DIR ) } } handlers_dict.update(custom_handler_item) - -if ENABLE_DEBUG_LOGGING: LOGGING = { 'version': 1, 'disable_existing_loggers': False, From 77209921f9d132cc18c453337f000dd90a4f9a32 Mon Sep 17 00:00:00 2001 From: PCoder Date: Fri, 1 Dec 2017 21:03:59 +0100 Subject: [PATCH 4/4] Some more refactoring --- dynamicweb/settings/base.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 3f40d8a7..67734052 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -586,20 +586,10 @@ if ENABLE_LOGGING: if MODULES_TO_LOG is None: # set MODULES_TO_LOG to django, if it is not set MODULES_TO_LOG = 'django' - if ',' in MODULES_TO_LOG: - modules_to_log_list = MODULES_TO_LOG.split(',') - for custom_module in modules_to_log_list: - logger_item = { - custom_module: { - 'handlers': ['custom_file'], - 'level': LOG_LEVEL, - 'propagate': True - } - } - loggers_dict.update(logger_item) - else: + modules_to_log_list = MODULES_TO_LOG.split(',') + for custom_module in modules_to_log_list: logger_item = { - MODULES_TO_LOG: { + custom_module: { 'handlers': ['custom_file'], 'level': LOG_LEVEL, 'propagate': True