From 137b45c12c500ceb5f4e1430099b19abf153fda3 Mon Sep 17 00:00:00 2001
From: "M.Ravi" <mondi.ravi@gmail.com>
Date: Wed, 29 Nov 2017 06:45:09 +0100
Subject: [PATCH] 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():
                    </CONTEXT>
                 </TEMPLATE>
                 """
-        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'],