add unified logger process for qtip 43/21543/4
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Tue, 20 Sep 2016 05:58:21 +0000 (13:58 +0800)
committerSerenaFeng <feng.xiaowei@zte.com.cn>
Wed, 21 Sep 2016 07:02:44 +0000 (15:02 +0800)
log the output information to both file and console, and
unify the log file path and level

JIRA: QTIP-108

Change-Id: I3a881e9da1f74c6959250a94f05cf90a231c34e6
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
docker/Dockerfile
func/ansible_api.py
func/driver.py
func/env_setup.py
utils/__init__.py [new file with mode: 0644]
utils/logger_utils.py [new file with mode: 0644]

index 2adeba6..fc0e57c 100644 (file)
@@ -43,6 +43,7 @@ RUN apt-get install ansible --force-yes -y
 
 RUN mkdir -p ${REPOS_DIR}
 RUN mkdir -p /root/.ssh
+RUN mkdir -p /var/log/qtip
 RUN chmod 700 /root/.ssh
 
 #Config ansible
index 57224eb..2f02a62 100644 (file)
@@ -8,12 +8,15 @@
 ##############################################################################
 import os
 from collections import namedtuple
-import logging
 
+from ansible.executor.playbook_executor import PlaybookExecutor
+from ansible.inventory import Inventory
 from ansible.parsing.dataloader import DataLoader
 from ansible.vars import VariableManager
-from ansible.inventory import Inventory
-from ansible.executor.playbook_executor import PlaybookExecutor
+
+from utils import logger_utils
+
+logger = logger_utils.QtipLogger('ansible_api').get
 
 
 class AnsibleApi:
@@ -26,7 +29,7 @@ class AnsibleApi:
 
     def _check_path(self, file_path):
         if not os.path.exists(file_path):
-            logging.error('The playbook %s does not exist' % file_path)
+            logger.error('The playbook %s does not exist' % file_path)
             return False
         else:
             return True
index 726016a..ff40a4c 100644 (file)
@@ -1,20 +1,22 @@
 ##############################################################################
-# Copyright (c) 2015 Dell Inc  and others.
+# Copyright (c) 2015 Dell Inc, ZTE  and others.
 #
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Apache License, Version 2.0
 # which accompanies this distribution, and is available at
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
-import logging
-from func.ansible_api import AnsibleApi
+from utils import logger_utils
+from ansible_api import AnsibleApi
+
+logger = logger_utils.QtipLogger('driver').get
 
 
 class Driver:
 
     def __init__(self):
 
-        logging.info("Class driver initialized\n")
+        logger.info("Class driver initialized\n")
         self.installer_username = {'fuel': 'root',
                                    'joid': 'ubuntu',
                                    'apex': 'heat-admin'}
@@ -58,7 +60,7 @@ class Driver:
         return special_json
 
     def run_ansible_playbook(self, benchmark, extra_vars):
-        logging.info(extra_vars)
+        logger.info(extra_vars)
         ansible_api = AnsibleApi()
         ansible_api.execute_playbook('./data/hosts',
                                      './benchmarks/playbooks/{0}.yaml'.format(benchmark),
index 96f984c..cd82f30 100644 (file)
@@ -8,18 +8,19 @@
 ##############################################################################
 
 import os
+import random
+import socket
 import sys
-from collections import defaultdict
-import yaml
 import time
-import paramiko
-import socket
+from collections import defaultdict
 from os.path import expanduser
-import random
-import logging
 
-LOG = logging.getLogger(__name__)
-LOG.setLevel(logging.DEBUG)
+import paramiko
+import yaml
+
+from utils import logger_utils
+
+logger = logger_utils.QtipLogger('env_setup').get
 
 
 class Env_setup:
@@ -100,7 +101,7 @@ class Env_setup:
 
     @staticmethod
     def fetch_compute_ips():
-        LOG.info("Fetch compute ips through installer")
+        logger.info("Fetch compute ips through installer")
         ips = []
 
         installer_type = str(os.environ['INSTALLER_TYPE'].lower())
@@ -112,18 +113,18 @@ class Env_setup:
 
         cmd = "bash ./func/fetch_compute_ips.sh -i %s -a %s" % \
             (installer_type, installer_ip)
-        LOG.info(cmd)
+        logger.info(cmd)
         os.system(cmd)
         home = expanduser("~")
         with open(home + "/ips.log", "r") as file:
             data = file.read()
         if data:
             ips.extend(data.rstrip('\n').split('\n'))
-        LOG.info("All compute ips: %s" % ips)
+        logger.info("All compute ips: %s" % ips)
         return ips
 
     def check_machine_ips(self, host_tag):
-        LOG.info("Check machine ips")
+        logger.info("Check machine ips")
         ips = self.fetch_compute_ips()
         ips_num = len(ips)
         num = len(host_tag)
@@ -137,7 +138,7 @@ class Env_setup:
                 if host_tag[hostlabel]['ip'] in ips:
                     info = "%s's ip %s is defined by test case yaml file" % \
                         (hostlabel, host_tag[hostlabel]['ip'])
-                    LOG.info(info)
+                    logger.info(info)
                 else:
                     err = "%s is not in %s" % (host_tag[hostlabel]['ip'], ips)
                     raise RuntimeError(err)
diff --git a/utils/__init__.py b/utils/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/utils/logger_utils.py b/utils/logger_utils.py
new file mode 100644 (file)
index 0000000..780696f
--- /dev/null
@@ -0,0 +1,65 @@
+##############################################################################
+# Copyright (c) 2016 ZTE Corp and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Logging levels:
+#  Level     Numeric value
+#  CRITICAL  50
+#  ERROR     40
+#  WARNING   30
+#  INFO      20
+#  DEBUG     10
+#  NOTSET    0
+#
+# Usage:
+#  from utils import logger_utils
+#  logger = logger_utils.QtipLogger(__file__).get
+#  logger.info("message to be shown with - INFO - ")
+#  logger.debug("message to be shown with - DEBUG -")
+##############################################################################
+
+import logging
+import os
+
+
+class Logger(object):
+    file_path = '/var/log'
+    formatter = logging.Formatter('%(asctime)s - %(name)s - '
+                                  '%(levelname)s - %(message)s')
+
+    def __init__(self, logger_name):
+
+        IF_DEBUG = os.getenv('IF_DEBUG')
+
+        self.logger_name = logger_name
+        self.logger = logging.getLogger(logger_name)
+        self.logger.propagate = 0
+        self.logger.setLevel(logging.DEBUG)
+
+        ch = logging.StreamHandler()
+        ch.setFormatter(self.formatter)
+        if IF_DEBUG is not None and IF_DEBUG.lower() == "true":
+            ch.setLevel(logging.DEBUG)
+        else:
+            ch.setLevel(logging.INFO)
+        self.logger.addHandler(ch)
+
+        hdlr = logging.FileHandler('%s/%s.log' % (self.file_path, logger_name))
+        hdlr.setFormatter(self.formatter)
+        hdlr.setLevel(logging.DEBUG)
+        self.logger.addHandler(hdlr)
+
+    @property
+    def get(self):
+        return self.logger
+
+
+class QtipLogger(Logger):
+    file_path = '/var/log/qtip'
+
+    def __init__(self, logger_name):
+        super(QtipLogger, self).__init__(logger_name)