Add logger in python-sdktests 36/111836/5
authorjardellos <thierry.hardy@orange.com>
Fri, 28 Aug 2020 09:27:47 +0000 (11:27 +0200)
committerThierry Hardy <thierry.hardy@orange.com>
Mon, 31 Aug 2020 11:49:40 +0000 (13:49 +0200)
Adding of logger to onaptests setting the stream and file handlers based
on settings.py
onapsdk should evolve to include the same code
This would avoid to add it in run.py
Simplification of the proposal based on remarks from Michal and Morgan
Issue-ID: TEST-252

Signed-off-by: jardellos <thierry.hardy@orange.com>
Change-Id: I5b7baf77580df916c4f5e62965a47d8462b44c43

run.py
setup.cfg
src/onaptests/configuration/settings.py
src/onaptests/steps/base.py

diff --git a/run.py b/run.py
index ec64125..144edde 100644 (file)
--- a/run.py
+++ b/run.py
@@ -1,19 +1,13 @@
-import logging
+import logging.config
+from onapsdk.configuration import settings
+from onaptests.steps.instantiate.service_ala_carte import ServiceAlaCarteInstantiateStep
 
-from onaptests.steps.instantiate.vf_module_ala_carte import YamlTemplateVfModuleAlaCarteInstantiateStep
-
-
-# Configure logging
-logger = logging.getLogger("")
-logger.setLevel(logging.INFO)
-fh = logging.StreamHandler()
-fh_formatter = logging.Formatter(
-    "%(asctime)s %(levelname)s %(name)s %(lineno)d:%(filename)s(%(process)d) - %(message)s"
-)
-fh.setFormatter(fh_formatter)
-logger.addHandler(fh)
 
 
 if __name__ == "__main__":
-    vf_module_inst = YamlTemplateVfModuleAlaCarteInstantiateStep()
-    vf_module_inst.execute()
+    # logging configuration for onapsdk, it is not requested for onaptests
+    # Correction requested in onapsdk to avoid having this duplicate code
+    logging.config.dictConfig(settings.LOG_CONFIG)
+
+    service_inst = ServiceAlaCarteInstantiateStep()
+    service_inst.execute()
index d5907f2..1db30bf 100644 (file)
--- a/setup.cfg
+++ b/setup.cfg
@@ -10,10 +10,12 @@ license = Apache 2.0
 classifiers =
   Programming Language :: Python :: 3
   Programming Language :: Python :: 3.7
+  Programming Language :: Python :: 3.8
 
 [options]
 zip_safe = False
 include_package_data = True
+python_requires = >=3.7,<4
 package_dir=
     =src
 packages=find_namespace:
index f6959de..0e69b4e 100644 (file)
@@ -6,6 +6,38 @@
 #                    #
 ######################
 
+
+# Variables to set logger information
+# Possible values for logging levels in onapsdk: INFO, DEBUG , WARNING, ERROR
+LOG_CONFIG = {
+    "version": 1,
+    "disable_existing_loggers": False,
+    "formatters": {
+        "default": {
+            "class": "logging.Formatter",
+            "format": "%(asctime)s %(levelname)s %(lineno)d:%(filename)s(%(process)d) - %(message)s"
+        }
+    },
+    "handlers": {
+        "console": {
+            "level": "DEBUG",
+            "class": "logging.StreamHandler",
+            "formatter": "default"
+        },
+        "file": {
+            "level": "DEBUG",
+            "class": "logging.FileHandler",
+            "formatter": "default",
+            "filename": "./pythonsdk.debug.log",
+            "mode": "w"
+        }
+    },
+    "root": {
+        "level": "DEBUG",
+        "handlers": ["console", "file"]
+    }
+}
+
 VENDOR_NAME = "sdktests_vendor"
 VSP_NAME = "sdktests_vsp"
 VSP_FILE_PATH = "vfw.zip"
index b32e6d3..5f66935 100644 (file)
@@ -1,10 +1,24 @@
+import logging
+import logging.config
+
 from abc import ABC, abstractmethod
 from typing import List
-
+from onapsdk.configuration import settings
 
 class BaseStep(ABC):
     """Base step class."""
 
+    _logger: logging.Logger = logging.getLogger("")
+
+    def __init_subclass__(cls):
+        """Subclass initialization.
+
+        Add _logger property for any BaseStep subclass
+        """
+        super().__init_subclass__()
+        cls._logger: logging.Logger = logging.getLogger("")
+        logging.config.dictConfig(settings.LOG_CONFIG)
+
     def __init__(self, cleanup: bool = False) -> None:
         """Step initialization.
 
@@ -16,6 +30,8 @@ class BaseStep(ABC):
         self._cleanup: bool = cleanup
         self._parent: "BaseStep" = None
 
+
+
     def add_step(self, step: "BaseStep") -> None:
         """Add substep.