Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set log level for all craft libs #111

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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