From 64d4078a80bbe99b93ee3f16606c13b769ba01d4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugo@lysator.liu.se>
Date: Tue, 21 Mar 2023 13:57:51 +0100
Subject: [PATCH] Fix logging.

* Make logging work
* Introduces the --log command line argument
* Attempt to use colorlog if it's installed.
---
 r11k/main.py             | 22 ++++++++++++++++++----
 r11k/puppetfile.py       |  5 +++--
 r11k/puppetmodule/git.py |  1 +
 r11k/util.py             |  2 +-
 4 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/r11k/main.py b/r11k/main.py
index 91d33a0..d9b8da6 100644
--- a/r11k/main.py
+++ b/r11k/main.py
@@ -31,6 +31,8 @@ def __main() -> int:
     parser.add_argument('--env-name', help='Force name of environment', dest='environment_name')
     parser.add_argument('--force', help='Refresh all caches', dest='force',
                         action='store_true')
+    parser.add_argument('--log', help='Set outptu verbosity',
+                        dest='loglevel', action='store')
 
     args = parser.parse_args()
 
@@ -38,12 +40,24 @@ def __main() -> int:
         r11k.config.config.http_ttl = 0
         r11k.config.config.git_ttl = 0
 
-    logger.setLevel(logging.DEBUG)
+    root_logger = logging.getLogger('')
+    root_logger.setLevel(logging.WARNING)
+    if args.loglevel:
+        root_logger.setLevel(args.loglevel)
+
     ch = logging.StreamHandler()
-    ch.setLevel(logging.DEBUG)
-    formatter = logging.Formatter('%(name)s %(lineno)i - %(levelname)s - %(message)s')
+
+    formatter: logging.Formatter
+    try:
+        import colorlog
+        formatter = colorlog.ColoredFormatter('%(log_color)s%(levelname)s:%(name)s:%(message)s')
+    except ModuleNotFoundError:
+        formatter = logging.Formatter('%(name)s %(lineno)i - %(levelname)s - %(message)s')
+
     ch.setFormatter(formatter)
-    logger.addHandler(ch)
+    root_logger.addHandler(ch)
+
+    root_logger.debug('Logging initialized')
 
     puppetfile = load_puppetfile(args.puppetfile)
 
diff --git a/r11k/puppetfile.py b/r11k/puppetfile.py
index f47a8de..4fd7ca6 100644
--- a/r11k/puppetfile.py
+++ b/r11k/puppetfile.py
@@ -166,7 +166,7 @@ class ResolvedPuppetFile(PuppetFile):
         logger.info('== Building actual environment ==')
         util.ensure_directory(destination)
         for module in self.modules.values():
-            logger.info(module)
+            logger.debug('Including %s', module)
             path = os.path.join(destination, 'modules', module.module_path)
             module.publish(path)
 
@@ -227,6 +227,7 @@ def parse_puppetfile(data: dict[str, Any],
 
 def load_puppetfile(filepath: str) -> PuppetFile:
     """Load puppetfile.yaml."""
+    logger.debug('Loading %s', filepath)
     with open(filepath) as f:
         data = yaml.full_load(f)
         return parse_puppetfile(data, path=filepath)
@@ -275,7 +276,7 @@ def find_all_modules_for_environment(puppetfile: PuppetFile) -> ResolvedPuppetFi
 
         # For each module in puppetfile
         for module in modules:
-            logger.debug(module)
+            logger.debug('Handling %s', module)
             # collect its dependencies
             for depspec in module.metadata.dependencies:
                 # and merge them to the dependency set
diff --git a/r11k/puppetmodule/git.py b/r11k/puppetmodule/git.py
index b937718..bfe6a73 100644
--- a/r11k/puppetmodule/git.py
+++ b/r11k/puppetmodule/git.py
@@ -141,6 +141,7 @@ class GitPuppetModule(PuppetModule):
         # NOTE force without a warning isn't the best idea, but will
         # have to do for now.
         checkout(repo, self.version, force=True)
+        logger.info('Published module %s', self)
 
     def versions(self) -> list[VersionInfo]:
         """
diff --git a/r11k/util.py b/r11k/util.py
index bf19410..774b46c 100644
--- a/r11k/util.py
+++ b/r11k/util.py
@@ -18,7 +18,7 @@ def ensure_directory(path: str) -> None:  # pragma: no cover
 
 def download_file(url: str, outfile: str) -> None:
     """Fetch file from url, save it to outfile."""
-    logger.info('Downloading', outfile)
+    logger.info('Downloading %s', outfile)
     r = requests.get(url, stream=True)
     if r.status_code != requests.codes.ok:
         r.raise_for_status()
-- 
GitLab