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

fix(logging): make the verbosity option more eager #6

Merged
merged 2 commits into from
Sep 26, 2024
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
1 change: 1 addition & 0 deletions src/clapper/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def callback(ctx, param, value):
f"option as often as desired (e.g. '-vvv' for debug)."
),
callback=callback,
is_eager=True, # Ensure the logger is set for ResourceOptions loading
**kwargs,
)(f)

Expand Down
9 changes: 9 additions & 0 deletions tests/data/complex.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import logging

logger = logging.getLogger("clapper_test.config_with_logs")

logger.debug("Debug level message")
logger.info("Info level message")
logger.warning("Warning level message")
logger.error("Error level message")

cplx = dict(
a="test",
b=42,
Expand Down
121 changes: 120 additions & 1 deletion tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import clapper.logging
import click
import pytest

from clapper.click import verbosity_option
from clapper.click import ConfigCommand, ResourceOption, verbosity_option
from click.testing import CliRunner


Expand Down Expand Up @@ -195,3 +196,121 @@ def cli(**_):

assert "debug message\ninfo message\n" in lo.getvalue()
assert hi.getvalue() == "warning message\nerror message\n"


# Testing the logger is also set correctly during the loading of config files.


@pytest.fixture
def cli_config():
messages = io.StringIO()
logger = clapper.logging.setup(
"clapper_test",
format="[%(levelname)s] %(message)s",
low_level_stream=messages,
high_level_stream=messages,
)
logger.setLevel("ERROR") # Enforce a default level

@click.command(entry_point_group="clapper.test.config", cls=ConfigCommand)
@click.option("--cmp", entry_point_group="clapper.test.config", cls=ResourceOption)
@verbosity_option(logger, expose_value=False)
def cli(**_):
"""This is the documentation provided by the user."""
pass

return (cli, messages)


# Loading configs using ResourceOption


def test_logger_click_option_config_q(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["--cmp", "complex-var"])
expected = "[ERROR] Error level message\n"
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_option_config_v(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["--cmp", "complex-var", "-v"])
expected = "[WARNING] Warning level message\n" "[ERROR] Error level message\n"
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_option_config_vv(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["--cmp", "complex-var", "-vv"])
expected = (
"[INFO] Info level message\n"
"[WARNING] Warning level message\n"
"[ERROR] Error level message\n"
)
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_option_config_vvv(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["--cmp", "complex-var", "-vvv"])
expected = (
"[DEBUG] Debug level message\n"
"[INFO] Info level message\n"
"[WARNING] Warning level message\n"
"[ERROR] Error level message\n"
)
assert result.exit_code == 0, result.output
assert log_output.getvalue().endswith(expected)


# Loading configs using ConfigCommand
def test_logger_click_command_config_q(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["complex"])
expected = "[ERROR] Error level message\n"
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_command_config_v(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["complex", "-v"])
expected = "[WARNING] Warning level message\n" "[ERROR] Error level message\n"
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_command_config_vv(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["complex", "-vv"])
expected = (
"[INFO] Info level message\n"
"[WARNING] Warning level message\n"
"[ERROR] Error level message\n"
)
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_command_config_vvv(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["complex", "-vvv"])
expected = (
"[DEBUG] Debug level message\n"
"[INFO] Info level message\n"
"[WARNING] Warning level message\n"
"[ERROR] Error level message\n"
)
assert result.exit_code == 0, result.output
assert log_output.getvalue().endswith(expected)
Loading