Add option to enable debug logs 79/12379/4
authorThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 24 Aug 2018 01:36:46 +0000 (21:36 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 24 Aug 2018 04:31:08 +0000 (00:31 -0400)
Default the logger to INFO level and add an option to turn on DEBUG
level logs. Update the unit_tests to pass in a context since we need
it now in order to support the top-level configuration.

Change-Id: I253a127f7dc0cee5475d8403da8f82e018fdf36e
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
docs/commands/index.rst
lftools/__init__.py
lftools/cli/__init__.py
releasenotes/notes/debug-e80d591d478e69cc.yaml [new file with mode: 0644]
tests/test_license.py
tests/test_version.py

index 2e81508..1a10b28 100644 (file)
@@ -3,7 +3,9 @@ Commands
 ########
 
 lftools is a collection of scripts written directly in python or externally via
-bash. It supports the following commands.
+bash.
+
+It supports the following commands:
 
 .. toctree::
     :maxdepth: 2
@@ -16,3 +18,7 @@ bash. It supports the following commands.
     openstack
     sign
     version
+
+Enable debugging via ``lftools --debug`` preceding any commands or via
+environment variable ``DEBUG=True``, this will print extra information if
+available.
index 2754d0c..f2d3b69 100644 (file)
@@ -22,5 +22,5 @@ import logging.config
 formatter = logging.Formatter('(%(levelname)s) %(name)s: %(message)s')
 console_handler = logging.StreamHandler()
 console_handler.setFormatter(formatter)
-logging.getLogger("").setLevel(logging.NOTSET)
+logging.getLogger("").setLevel(logging.INFO)
 logging.getLogger("").addHandler(console_handler)
index 25fc94e..8b95967 100644 (file)
@@ -11,6 +11,8 @@
 
 __author__ = 'Thanh Ha'
 
+import logging
+
 import click
 
 from lftools.cli.config import config_sys
@@ -22,13 +24,20 @@ from lftools.cli.nexus import nexus
 from lftools.cli.sign import sign
 from lftools.cli.version import version
 
+log = logging.getLogger(__name__)
+
 
 @click.group()
+@click.option('--debug', envvar='DEBUG', is_flag=True, default=False)
 @click.pass_context
 @click.version_option()
-def cli(ctx):
+def cli(ctx, debug):
     """CLI entry point for lftools."""
-    pass
+    if debug:
+        logging.getLogger("").setLevel(logging.DEBUG)
+
+    ctx.obj['DEBUG'] = debug
+    log.debug('DEBUG mode enabled.')
 
 
 cli.add_command(config_sys)
diff --git a/releasenotes/notes/debug-e80d591d478e69cc.yaml b/releasenotes/notes/debug-e80d591d478e69cc.yaml
new file mode 100644 (file)
index 0000000..fc332b2
--- /dev/null
@@ -0,0 +1,5 @@
+---
+features:
+  - |
+    Add a new ``--debug`` flag to enable extra troubleshooting information.
+    This flag can also be set via environment variable ``DEBUG=True``.
index 08d1263..ef2ba82 100644 (file)
@@ -29,12 +29,12 @@ def test_check_license(cli_runner, datafiles):
     os.chdir(str(datafiles))
 
     # Check that license checker passes when file has license.
-    result = cli_runner.invoke(cli.cli, ['license', 'check', 'license.py'])
+    result = cli_runner.invoke(cli.cli, ['license', 'check', 'license.py'], obj={})
     # noqa: B101 .
     assert result.exit_code == 0
 
     # Check that license checker fails when file is missing license.
-    result = cli_runner.invoke(cli.cli, ['license', 'check', 'no_license1.py'])
+    result = cli_runner.invoke(cli.cli, ['license', 'check', 'no_license1.py'], obj={})
     # noqa: B101 .
     assert result.exit_code == 1
 
@@ -48,13 +48,13 @@ def test_check_license_directory(cli_runner, datafiles):
 
     # Check that check-dir fails due to directory containing files
     # with no license.
-    result = cli_runner.invoke(cli.cli, ['license', 'check-dir', '.'])
+    result = cli_runner.invoke(cli.cli, ['license', 'check-dir', '.'], obj={})
     # noqa: B101 .
     assert result.exit_code == 1
 
     # Check that check-dir passes when directory contains files with licenses
     os.remove('no_license1.py')
     os.remove('no_license2.py')
-    result = cli_runner.invoke(cli.cli, ['license', 'check-dir', '.'])
+    result = cli_runner.invoke(cli.cli, ['license', 'check-dir', '.'], obj={})
     # noqa: B101 .
     assert result.exit_code == 0
index 10678e4..a34ac86 100644 (file)
@@ -29,7 +29,7 @@ FIXTURE_DIR = os.path.join(
 def test_version_bump(cli_runner, datafiles):
     """Test version bump command."""
     os.chdir(str(datafiles))
-    cli_runner.invoke(cli.cli, ['version', 'bump', 'TestRelease'])
+    cli_runner.invoke(cli.cli, ['version', 'bump', 'TestRelease'], obj={})
 
     for _file in datafiles.listdir():
         pom = str(_file) + '/pom.xml'
@@ -44,7 +44,7 @@ def test_version_bump(cli_runner, datafiles):
 def test_version_release(cli_runner, datafiles):
     """Test version release command."""
     os.chdir(str(datafiles))
-    cli_runner.invoke(cli.cli, ['version', 'release', 'TestRelease'])
+    cli_runner.invoke(cli.cli, ['version', 'release', 'TestRelease'], obj={})
 
     for _file in datafiles.listdir():
         pom = str(_file) + '/pom.xml'