From 6183cefebbc1af8ad1987dc61372d6c2d6a59f6d Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Tue, 20 Sep 2016 13:58:21 +0800 Subject: add unified logger process for qtip 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 --- docker/Dockerfile | 1 + func/ansible_api.py | 11 +++++---- func/driver.py | 12 ++++++---- func/env_setup.py | 27 ++++++++++----------- utils/__init__.py | 0 utils/logger_utils.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 94 insertions(+), 22 deletions(-) create mode 100644 utils/__init__.py create mode 100644 utils/logger_utils.py diff --git a/docker/Dockerfile b/docker/Dockerfile index 2adeba6e..fc0e57c5 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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 diff --git a/func/ansible_api.py b/func/ansible_api.py index 57224eb7..2f02a62e 100644 --- a/func/ansible_api.py +++ b/func/ansible_api.py @@ -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 diff --git a/func/driver.py b/func/driver.py index 726016a5..ff40a4cc 100644 --- a/func/driver.py +++ b/func/driver.py @@ -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), diff --git a/func/env_setup.py b/func/env_setup.py index 96f984cb..cd82f303 100644 --- a/func/env_setup.py +++ b/func/env_setup.py @@ -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 index 00000000..e69de29b diff --git a/utils/logger_utils.py b/utils/logger_utils.py new file mode 100644 index 00000000..780696f4 --- /dev/null +++ b/utils/logger_utils.py @@ -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) -- cgit 1.2.3-korg