diff options
author | Helen Yao <yaohelan@huawei.com> | 2017-01-23 09:05:25 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-01-23 09:05:25 +0000 |
commit | 1015e2f16285c24fb510c9b937bcef593c940ae8 (patch) | |
tree | db7e30de2a6b3b8d3f27c96798b4b96d0aeec61e /functest/utils | |
parent | 6503cfae235e53fc994d84411cc8f3d8a59750fd (diff) | |
parent | 168502f9fa58d9c348adb563b705d17c5ec4dcf7 (diff) |
Merge "Leverage logging config and refactor the logger"
Diffstat (limited to 'functest/utils')
-rwxr-xr-x[-rw-r--r--] | functest/utils/functest_logger.py | 60 |
1 files changed, 37 insertions, 23 deletions
diff --git a/functest/utils/functest_logger.py b/functest/utils/functest_logger.py index c0fba082..f09f56be 100644..100755 --- a/functest/utils/functest_logger.py +++ b/functest/utils/functest_logger.py @@ -20,36 +20,50 @@ # logger = fl.Logger("script_name").getLogger() # logger.info("message to be shown with - INFO - ") # logger.debug("message to be shown with - DEBUG -") - import logging +import logging.config import os +import json -class Logger: - def __init__(self, logger_name): +from functest.utils.constants import CONST + +logger = logging.getLogger(__name__) + + +def is_debug(): + if CONST.CI_DEBUG and CONST.CI_DEBUG.lower() == "true": + return True + return False - CI_DEBUG = os.getenv('CI_DEBUG') +def setup_logging(default_path=CONST.dir_functest_logging_cfg, + default_level=logging.INFO, + env_key='LOG_CFG'): + path = default_path + value = os.getenv(env_key, None) + if value: + path = value + if os.path.exists(path): + with open(path, 'rt') as f: + config = json.load(f) + if (config['handlers'] and + config['handlers']['console']): + stream_level = logging.INFO + if is_debug(): + stream_level = logging.DEBUG + config['handlers']['console']['level'] = stream_level + logging.config.dictConfig(config) + else: + logging.basicConfig(level=default_level) + + +setup_logging() + + +class Logger: + def __init__(self, logger_name): self.logger = logging.getLogger(logger_name) - self.logger.propagate = 0 - self.logger.setLevel(logging.DEBUG) - - ch = logging.StreamHandler() - formatter = logging.Formatter('%(asctime)s - %(name)s - ' - '%(levelname)s - %(message)s') - ch.setFormatter(formatter) - if CI_DEBUG is not None and CI_DEBUG.lower() == "true": - ch.setLevel(logging.DEBUG) - self.logger.parent.level = logging.DEBUG - else: - ch.setLevel(logging.INFO) - self.logger.parent.level = logging.INFO - self.logger.addHandler(ch) - - hdlr = logging.FileHandler('/home/opnfv/functest/results/functest.log') - hdlr.setFormatter(formatter) - hdlr.setLevel(logging.DEBUG) - self.logger.addHandler(hdlr) def getLogger(self): return self.logger |