From d8d9e5711f45ba94c0740bb0ef6032f42d97bf43 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 6 Jan 2022 16:59:42 +0100 Subject: [PATCH] Changing logging --- lib/cr_vmware_tpl/app.py | 52 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/cr_vmware_tpl/app.py b/lib/cr_vmware_tpl/app.py index 74bbc0f..9d50b0a 100644 --- a/lib/cr_vmware_tpl/app.py +++ b/lib/cr_vmware_tpl/app.py @@ -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') -- 2.39.5