From 12baf340e627083243e05b34c11826e6250b9c1f Mon Sep 17 00:00:00 2001
From: Michal Bocek <mbocek@redhat.com>
Date: Tue, 10 Mar 2020 21:05:06 +0100
Subject: [PATCH] Unify logger.conf and the fallback code

- use logging.Formatter.converter = time.gmtime for both the file config and fallback
  config to have the same (UTC) time, in both cases
- unify log levels
---
 leapp/libraries/stdlib/__init__.py |  3 +-
 leapp/logger/__init__.py           | 71 +++++++++++++++++++++---------
 leapp/snactor/context.py           |  2 +-
 3 files changed, 52 insertions(+), 24 deletions(-)

diff --git a/leapp/libraries/stdlib/__init__.py b/leapp/libraries/stdlib/__init__.py
index 95974964a..117c50dd3 100644
--- a/leapp/libraries/stdlib/__init__.py
+++ b/leapp/libraries/stdlib/__init__.py
@@ -151,7 +151,8 @@ def run(args, split=False, callback_raw=_console_logging_handler, callback_lineb
     """
     Run a command and return its result as a dict.
 
-    The execution of the program and it's results are captured by the audit.
+    The execution of the command and its result is captured by the audit. The CalledProcessError is raised when
+    the command exits with non-zero code.
 
     :param args: Command to execute
     :type args: list or tuple
diff --git a/leapp/logger/__init__.py b/leapp/logger/__init__.py
index b38f5035b..81e13e319 100644
--- a/leapp/logger/__init__.py
+++ b/leapp/logger/__init__.py
@@ -10,7 +10,7 @@
 from leapp.utils.actorapi import get_actor_api, RequestException
 from leapp.utils.audit import Audit
 
-_logger = None
+_leapp_logger = None
 
 
 class LeappAuditHandler(logging.Handler):
@@ -52,39 +52,66 @@ def _remote_emit(self, log_data):
 
 
 def configure_logger(log_file=None):
-    global _logger
-    if not _logger:
+    """
+    Configure loggers as per the description below.
+
+    logger: root
+      level: DEBUG
+      handler: StreamHandler
+        level: based on the debug/verbosity options
+      handler: LeappAuditHandler
+        level: DEBUG
+      logger: urllib3
+        level: WARN
+      logger: leapp
+        level: NOTSET
+        handler: FileHandler
+          level: DEBUG
+
+    :return: The 'leapp' logger
+    """
+    global _leapp_logger
+    if not _leapp_logger:
+        _leapp_logger = logging.getLogger('leapp')
+
         log_format = '%(asctime)s.%(msecs)-3d %(levelname)-8s PID: %(process)d %(name)s: %(message)s'
         log_date_format = '%Y-%m-%d %H:%M:%S'
-        path = os.getenv('LEAPP_LOGGER_CONFIG', '/etc/leapp/logger.conf')
+        logging.Formatter.converter = time.gmtime
 
+        root_logger = logging.getLogger()
+        root_logger.setLevel(logging.DEBUG)
+
+        path = os.getenv('LEAPP_LOGGER_CONFIG', '/etc/leapp/logger.conf')
         if path and os.path.isfile(path):
             logging.config.fileConfig(path)
         else:  # Fall back logging configuration
-            logging.Formatter.converter = time.gmtime
-            logging.basicConfig(
-                level=logging.ERROR,
-                format=log_format,
-                datefmt=log_date_format,
-                stream=sys.stderr,
-            )
+            stderr_handler = logging.StreamHandler(sys.stderr)
+            stderr_handler.setFormatter(logging.Formatter(fmt=log_format, datefmt=log_date_format))
+            stderr_handler.setLevel(logging.ERROR)
+            root_logger.addHandler(stderr_handler)
+
             logging.getLogger('urllib3').setLevel(logging.WARN)
-            handler = LeappAuditHandler()
-            handler.setFormatter(logging.Formatter(fmt=log_format, datefmt=log_date_format))
-            logging.getLogger('leapp').addHandler(handler)
+
+            audit_handler = LeappAuditHandler()
+            audit_handler.setFormatter(logging.Formatter(fmt=log_format, datefmt=log_date_format))
+            audit_handler.setLevel(logging.DEBUG)
+            root_logger.addHandler(audit_handler)
 
         if log_file:
             file_handler = logging.FileHandler(os.path.join(get_config().get('logs', 'dir'), log_file))
             file_handler.setFormatter(logging.Formatter(fmt=log_format, datefmt=log_date_format))
             file_handler.setLevel(logging.DEBUG)
-            logging.getLogger('leapp').addHandler(file_handler)
+            _leapp_logger.addHandler(file_handler)
 
-        if is_verbose():
-            for handler in logging.getLogger().handlers:
-                if isinstance(handler, logging.StreamHandler):
-                    handler.setLevel(logging.DEBUG if is_debug() else logging.INFO)
+        for handler in root_logger.handlers:
+            if isinstance(handler, logging.StreamHandler):
+                if is_debug():
+                    handler.setLevel(logging.DEBUG)
+                elif is_verbose():
+                    handler.setLevel(logging.INFO)
+                else:
+                    handler.setLevel(logging.ERROR)
 
-        _logger = logging.getLogger('leapp')
-        _logger.info('Logging has been initialized')
+        _leapp_logger.info('Logging has been initialized')
 
-    return _logger
+    return _leapp_logger
diff --git a/leapp/snactor/context.py b/leapp/snactor/context.py
index d3ab66502..b3673b045 100644
--- a/leapp/snactor/context.py
+++ b/leapp/snactor/context.py
@@ -10,7 +10,7 @@ def last_snactor_context(connection=None):
     Retrieves the last snactor-run context from the database. It generates a new one if none has been found.
 
     :param connection: Database connection to use instead of the default connection.
-    :returns: String representing the latest snactor-run context uuid.
+    :return: String representing the latest snactor-run context uuid.
     """
     with get_connection(db=connection) as db:
         cursor = db.execute('''