ariatosca-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mxm...@apache.org
Subject [1/2] incubator-ariatosca git commit: Improve logging from cli tests [Forced Update!]
Date Wed, 12 Apr 2017 15:44:25 GMT
Repository: incubator-ariatosca
Updated Branches:
  refs/heads/ARIA-138-Make-logging-more-informative ba237ebad -> 6142dc44f (forced update)


Improve logging from cli tests


Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/e1b682b2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/e1b682b2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/e1b682b2

Branch: refs/heads/ARIA-138-Make-logging-more-informative
Commit: e1b682b2e33d6f6cae5829a55210929c3e909793
Parents: 5b245b4
Author: Avia Efrat <avia@gigaspaces.com>
Authored: Mon Apr 10 15:32:25 2017 +0300
Committer: Avia Efrat <avia@gigaspaces.com>
Committed: Mon Apr 10 15:32:25 2017 +0300

----------------------------------------------------------------------
 aria/cli/logger.py     |  3 ++-
 tests/cli/base_test.py | 24 ++++++++++++++++++++++--
 tests/cli/utils.py     | 30 +++++++++++++-----------------
 3 files changed, 37 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/e1b682b2/aria/cli/logger.py
----------------------------------------------------------------------
diff --git a/aria/cli/logger.py b/aria/cli/logger.py
index 289dbd3..cbb916d 100644
--- a/aria/cli/logger.py
+++ b/aria/cli/logger.py
@@ -47,7 +47,8 @@ DEFAULT_LOGGER_CONFIG = {
             "stream": "ext://sys.stdout",
             "formatter": "console"
         }
-    }
+    },
+    "disable_existing_loggers": False
 }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/e1b682b2/tests/cli/base_test.py
----------------------------------------------------------------------
diff --git a/tests/cli/base_test.py b/tests/cli/base_test.py
index 1dae957..783631b 100644
--- a/tests/cli/base_test.py
+++ b/tests/cli/base_test.py
@@ -1,13 +1,27 @@
 from StringIO import StringIO
-
+import logging
+#
 import runner
 from utils import setup_logger
+import pytest
 
 
+@pytest.mark.usefixtures("redirect_logger")
 class TestCliBase(object):
 
+    @staticmethod
+    @pytest.fixture(scope="class")
+    def redirect_logger():
+
+        setup_logger(logger_name='aria.cli.main',
+                     handlers=[logging.StreamHandler(TestCliBase._logger_output)],
+                     logger_format='%(message)s')
+        yield
+        setup_logger(logger_name='aria.cli.main',
+                     handlers=_default_logger_config['handlers'],
+                     level=_default_logger_config['level'])
+
     _logger_output = StringIO()
-    setup_logger(logger_name='aria.cli.main', output_stream=_logger_output)
 
     def invoke(self, command):
         self._logger_output.truncate(0)
@@ -34,5 +48,11 @@ def raise_exception(exception, msg=''):
     return inner
 
 
+def get_default_logger_config():
+    logger = logging.getLogger('aria.cli.main')
+    return {'handlers': logger.handlers,
+            'level': logger.level}
+
+_default_logger_config = get_default_logger_config()
 
 

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/e1b682b2/tests/cli/utils.py
----------------------------------------------------------------------
diff --git a/tests/cli/utils.py b/tests/cli/utils.py
index fb3757a..7093e34 100644
--- a/tests/cli/utils.py
+++ b/tests/cli/utils.py
@@ -2,14 +2,14 @@ import logging
 
 
 def setup_logger(logger_name,
-                 output_stream,
-                 logger_level=logging.INFO,
+                 level=logging.INFO,
                  handlers=None,
                  remove_existing_handlers=True,
-                 logger_format=None):
+                 logger_format=None,
+                 propagate=True):
     """
     :param logger_name: Name of the logger.
-    :param logger_level: Level for the logger (not for specific handler).
+    :param level: Level for the logger (not for specific handler).
     :param handlers: An optional list of handlers (formatter will be
                      overridden); If None, only a StreamHandler for
                      sys.stdout will be used.
@@ -20,25 +20,21 @@ def setup_logger(logger_name,
     :return: A logger instance.
     :rtype: logging.Logger
     """
-    logger = logging.getLogger(logger_name)
-
-    if logger_format is None:
-        # for easier parsing in tests, we prefer that the log will only include the message.
-        logger_format = '%(message)s'
 
-    formatter = logging.Formatter(fmt=logger_format)
+    logger = logging.getLogger(logger_name)
 
     if remove_existing_handlers:
         for handler in logger.handlers:
             logger.removeHandler(handler)
 
-    if not handlers:
-        handler = logging.StreamHandler(output_stream)
-        handler.setLevel(logging.DEBUG)
-        handlers = [handler]
-
     for handler in handlers:
-        handler.setFormatter(formatter)
+        if logger_format:
+            formatter = logging.Formatter(fmt=logger_format)
+            handler.setFormatter(formatter)
         logger.addHandler(handler)
 
-    logger.setLevel(logger_level)
+    logger.setLevel(level)
+    if not propagate:
+        logger.propagate = False
+
+    return logger


Mime
View raw message