aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHelen Yao <yaohelan@huawei.com>2017-01-23 09:05:25 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-01-23 09:05:25 +0000
commit1015e2f16285c24fb510c9b937bcef593c940ae8 (patch)
treedb7e30de2a6b3b8d3f27c96798b4b96d0aeec61e
parent6503cfae235e53fc994d84411cc8f3d8a59750fd (diff)
parent168502f9fa58d9c348adb563b705d17c5ec4dcf7 (diff)
Merge "Leverage logging config and refactor the logger"
-rwxr-xr-xfunctest/ci/config_functest.yaml1
-rw-r--r--functest/ci/logging.json29
-rw-r--r--functest/tests/unit/cli/commands/test_cli_env.py1
-rwxr-xr-x[-rw-r--r--]functest/tests/unit/core/test_testcase_base.py2
-rw-r--r--functest/tests/unit/odl/test_odl.py1
-rw-r--r--functest/tests/unit/test_logging.ini27
-rw-r--r--functest/tests/unit/utils/test_functest_utils.py1
-rwxr-xr-x[-rw-r--r--]functest/utils/functest_logger.py60
-rwxr-xr-xrun_unit_tests.sh1
9 files changed, 95 insertions, 28 deletions
diff --git a/functest/ci/config_functest.yaml b/functest/ci/config_functest.yaml
index 25be1724..f1efd904 100755
--- a/functest/ci/config_functest.yaml
+++ b/functest/ci/config_functest.yaml
@@ -30,6 +30,7 @@ general:
functest: /home/opnfv/functest
functest_test: /home/opnfv/repos/functest/functest/opnfv_tests
results: /home/opnfv/functest/results
+ functest_logging_cfg: /home/opnfv/repos/functest/functest/ci/logging.json
functest_conf: /home/opnfv/functest/conf
functest_data: /home/opnfv/functest/data
dir_vIMS_data: /home/opnfv/functest/data/vIMS/
diff --git a/functest/ci/logging.json b/functest/ci/logging.json
new file mode 100644
index 00000000..3f454e8f
--- /dev/null
+++ b/functest/ci/logging.json
@@ -0,0 +1,29 @@
+{
+ "version": 1,
+ "disable_existing_loggers": false,
+ "formatters": {
+ "standard": {
+ "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
+ }
+ },
+ "handlers": {
+ "console": {
+ "level": "INFO",
+ "class": "logging.StreamHandler",
+ "formatter": "standard"
+ },
+ "file": {
+ "level": "DEBUG",
+ "class": "logging.FileHandler",
+ "formatter": "standard",
+ "filename": "/home/opnfv/functest/results/functest.log"
+ }
+ },
+ "loggers": {
+ "": {
+ "handlers": ["console", "file"],
+ "level": "DEBUG",
+ "propagate": "yes"
+ }
+ }
+}
diff --git a/functest/tests/unit/cli/commands/test_cli_env.py b/functest/tests/unit/cli/commands/test_cli_env.py
index f70761dc..4b6ea57a 100644
--- a/functest/tests/unit/cli/commands/test_cli_env.py
+++ b/functest/tests/unit/cli/commands/test_cli_env.py
@@ -11,7 +11,6 @@ import unittest
from git.exc import NoSuchPathError
import mock
-mock.patch('logging.FileHandler').start() # noqa
from functest.cli.commands import cli_env
from functest.utils.constants import CONST
from functest.tests.unit import test_utils
diff --git a/functest/tests/unit/core/test_testcase_base.py b/functest/tests/unit/core/test_testcase_base.py
index 8df524b0..94d2e966 100644..100755
--- a/functest/tests/unit/core/test_testcase_base.py
+++ b/functest/tests/unit/core/test_testcase_base.py
@@ -12,8 +12,6 @@ import mock
import os
import unittest
-mock.patch('logging.FileHandler').start() # noqa
-
from functest.core import testcase_base
diff --git a/functest/tests/unit/odl/test_odl.py b/functest/tests/unit/odl/test_odl.py
index 59ab2c65..568fdc82 100644
--- a/functest/tests/unit/odl/test_odl.py
+++ b/functest/tests/unit/odl/test_odl.py
@@ -19,7 +19,6 @@ from robot.errors import DataError, RobotError
from robot.result import testcase
from robot.utils.robottime import timestamp_to_secs
-mock.patch('logging.FileHandler').start() # noqa
from functest.core import testcase_base
from functest.opnfv_tests.sdn.odl import odl
diff --git a/functest/tests/unit/test_logging.ini b/functest/tests/unit/test_logging.ini
new file mode 100644
index 00000000..3d5b947c
--- /dev/null
+++ b/functest/tests/unit/test_logging.ini
@@ -0,0 +1,27 @@
+[loggers]
+keys=root,functest_logger
+
+[logger_root]
+level=DEBUG
+handlers=console
+
+[logger_functest_logger]
+level=DEBUG
+handlers=console
+qualname=functest.utils.functest_logger
+propagate=0
+
+[handlers]
+keys=console
+
+[handler_console]
+class=StreamHandler
+level=INFO
+formatter=standard
+args=(sys.stdout,)
+
+[formatters]
+keys=standard
+
+[formatter_standard]
+format=%(asctime)s - %(name)s - %(levelname)s - %(message)s \ No newline at end of file
diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py
index c4b56660..ce9086a7 100644
--- a/functest/tests/unit/utils/test_functest_utils.py
+++ b/functest/tests/unit/utils/test_functest_utils.py
@@ -18,7 +18,6 @@ import mock
import requests
from functest.tests.unit import test_utils
-mock.patch('logging.FileHandler').start() # noqa
from functest.utils import functest_utils
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
diff --git a/run_unit_tests.sh b/run_unit_tests.sh
index 79d05d3d..5167d78c 100755
--- a/run_unit_tests.sh
+++ b/run_unit_tests.sh
@@ -38,6 +38,7 @@ nosetests --with-xunit \
--cover-package=functest.utils \
--cover-xml \
--cover-html \
+ --log-config=$(pwd)/functest/tests/unit/test_logging.ini \
functest/tests/unit
rc=$?