From: Thanh Ha Date: Fri, 24 Aug 2018 01:36:46 +0000 (-0400) Subject: Add option to enable debug logs X-Git-Tag: v0.16.0~2 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F79%2F12379%2F4;p=releng%2Flftools.git Add option to enable debug logs 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 --- diff --git a/docs/commands/index.rst b/docs/commands/index.rst index 2e815089..1a10b280 100644 --- a/docs/commands/index.rst +++ b/docs/commands/index.rst @@ -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. diff --git a/lftools/__init__.py b/lftools/__init__.py index 2754d0c8..f2d3b692 100644 --- a/lftools/__init__.py +++ b/lftools/__init__.py @@ -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) diff --git a/lftools/cli/__init__.py b/lftools/cli/__init__.py index 25fc94e2..8b95967c 100644 --- a/lftools/cli/__init__.py +++ b/lftools/cli/__init__.py @@ -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 index 00000000..fc332b26 --- /dev/null +++ b/releasenotes/notes/debug-e80d591d478e69cc.yaml @@ -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``. diff --git a/tests/test_license.py b/tests/test_license.py index 08d1263b..ef2ba828 100644 --- a/tests/test_license.py +++ b/tests/test_license.py @@ -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 diff --git a/tests/test_version.py b/tests/test_version.py index 10678e4c..a34ac865 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -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'