diff --git a/craft_application/application.py b/craft_application/application.py index 5bc5c3e2..91cd9094 100644 --- a/craft_application/application.py +++ b/craft_application/application.py @@ -18,6 +18,7 @@ import functools import importlib +import logging import os import pathlib import signal @@ -244,6 +245,14 @@ def _get_dispatcher(self) -> craft_cli.Dispatcher: :returns: A ready-to-run Dispatcher object """ + # Set the logging level to DEBUG for all craft-libraries. This is OK even if + # the specific application doesn't use a specific library, the call does not + # import the package. + craft_libs = ["craft_archives", "craft_parts", "craft_providers", "craft_store"] + for craft_lib in craft_libs: + logger = logging.getLogger(craft_lib) + logger.setLevel(logging.DEBUG) + craft_cli.emit.init( mode=craft_cli.EmitterMode.BRIEF, appname=self.app.name, diff --git a/tests/unit/test_application.py b/tests/unit/test_application.py index 2c02b104..0e05a496 100644 --- a/tests/unit/test_application.py +++ b/tests/unit/test_application.py @@ -17,6 +17,7 @@ import argparse import importlib import importlib.metadata +import logging import pathlib import re import subprocess @@ -298,6 +299,23 @@ def test_get_dispatcher_error( check.is_true(re.fullmatch(message, captured.err), captured.err) +def test_craft_lib_log_level(app): + craft_libs = ["craft_archives", "craft_parts", "craft_providers", "craft_store"] + + # The logging module is stateful and global, so first lets clear the logging level + # that another test might have already set. + for craft_lib in craft_libs: + logger = logging.getLogger(craft_lib) + logger.setLevel(logging.NOTSET) + + with pytest.raises(SystemExit): + app._get_dispatcher() + + for craft_lib in craft_libs: + logger = logging.getLogger(craft_lib) + assert logger.level == logging.DEBUG + + @pytest.mark.parametrize( "argv", [["testcraft", "--version"], ["testcraft", "-V"], ["testcraft", "pull", "-V"]],