Skip to content

Commit

Permalink
feat: set log level for all craft libs
Browse files Browse the repository at this point in the history
  • Loading branch information
tigarmo committed Oct 23, 2023
1 parent 1bbc367 commit a243c94
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions craft_application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import functools
import importlib
import logging
import os
import pathlib
import signal
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import argparse
import importlib
import importlib.metadata
import logging
import pathlib
import re
import subprocess
Expand Down Expand Up @@ -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"]],
Expand Down

0 comments on commit a243c94

Please sign in to comment.