]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Changing logging
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 6 Jan 2022 15:59:42 +0000 (16:59 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 6 Jan 2022 15:59:42 +0000 (16:59 +0100)
lib/cr_vmware_tpl/app.py

index 74bbc0f8f1bca5629f4dea7be0ef212754cfbf75..9d50b0ae555400abc4b084ed5317a14b49bd8f10 100644 (file)
@@ -20,6 +20,8 @@ import pathlib
 # Own modules
 from . import __version__ as GLOBAL_VERSION
 
+from fb_logging.colored import ColoredFormatter
+
 from fb_tools.common import pp
 
 from fb_tools.app import BaseApplication
@@ -167,7 +169,55 @@ class CrTplApplication(BaseApplication):
     def init_logging(self):
         """Initialize the logger object."""
 
-        super(CrTplApplication, self).init_logging()
+        # super(CrTplApplication, self).init_logging()
+
+        log_level = logging.INFO
+        if self.verbose:
+            log_level = logging.DEBUG
+        elif self.quiet:
+            log_level = logging.WARNING
+
+        root_logger = logging.getLogger()
+        root_logger.setLevel(log_level)
+
+        # create formatter
+        format_str = ''
+        if self.verbose:
+            format_str = '[%(asctime)s]: '
+        format_str += self.appname + ': '
+        if self.verbose:
+            if self.verbose > 1:
+                format_str += '%(name)s(%(lineno)d) %(funcName)s() '
+            else:
+                format_str += '%(name)s '
+        format_str += '%(levelname)s - %(message)s'
+        formatter = None
+        if self.terminal_has_colors:
+            formatter = ColoredFormatter(format_str)
+        else:
+            formatter = logging.Formatter(format_str)
+
+        # ---------------------
+        class StdoutFilter(logging.Filter):
+            def filter(self, record):
+                return record.levelno in (logging.DEBUG, logging.INFO)
+
+        if not self.quiet:
+
+            # create log handler for console stdout
+            lh_stdout = logging.StreamHandler(sys.stdout)
+            lh_stdout.setLevel(log_level)
+            lh_stdout.addFilter(StdoutFilter())
+            lh_stdout.setFormatter(formatter)
+
+            root_logger.addHandler(lh_stdout)
+
+        # create log handler for console stderr
+        lh_stderr = logging.StreamHandler(sys.stderr)
+        lh_stderr.setLevel(logging.WARNING)
+        lh_stderr.setFormatter(formatter)
+
+        root_logger.addHandler(lh_stderr)
 
         if self.verbose < 4:
             paramiko_logger = logging.getLogger('paramiko.transport')