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: logger verbosity issues #2224

Merged
merged 4 commits into from
Aug 19, 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
8 changes: 4 additions & 4 deletions src/ape/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def abort(msg: str, base_error: Optional[Exception] = None) -> NoReturn:


def verbosity_option(
cli_logger: Optional[ApeLogger] = None, default: Union[str, LogLevel] = DEFAULT_LOG_LEVEL
cli_logger: Optional[ApeLogger] = None, default: Union[str, int, LogLevel] = DEFAULT_LOG_LEVEL
) -> Callable:
"""A decorator that adds a `--verbosity, -v` option to the decorated
command.
Expand All @@ -67,7 +67,7 @@ def verbosity_option(


def _create_verbosity_kwargs(
_logger: Optional[ApeLogger] = None, default: Union[str, LogLevel] = DEFAULT_LOG_LEVEL
_logger: Optional[ApeLogger] = None, default: Union[str, int, LogLevel] = DEFAULT_LOG_LEVEL
) -> dict:
cli_logger = _logger or logger

Expand All @@ -87,7 +87,7 @@ def set_level(ctx, param, value):


def ape_cli_context(
default_log_level: Union[str, LogLevel] = DEFAULT_LOG_LEVEL,
default_log_level: Union[str, int, LogLevel] = DEFAULT_LOG_LEVEL,
obj_type: type = ApeCliContextObject,
) -> Callable:
"""
Expand All @@ -96,7 +96,7 @@ def ape_cli_context(
such as logging or accessing managers.

Args:
default_log_level (Union[str, :class:`~ape.logging.LogLevel`]): The log-level
default_log_level (Union[str, int, :class:`~ape.logging.LogLevel`]): The log-level
value to pass to :meth:`~ape.cli.options.verbosity_option`.
obj_type (Type): The context object type. Defaults to
:class:`~ape.cli.options.ApeCliContextObject`. Sub-class
Expand Down
10 changes: 7 additions & 3 deletions src/ape/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,19 @@ def _load_from_sys_argv(self, default: Optional[Union[str, int, LogLevel]] = Non
def level(self) -> int:
return self._logger.level

def set_level(self, level: Union[str, int]):
def set_level(self, level: Union[str, int, LogLevel]):
"""
Change the global ape logger log-level.

Args:
level (str): The name of the level or the value of the log-level.
"""

if level == self._logger.level:
return

elif isinstance(level, LogLevel):
level = level.value

self._logger.setLevel(level)
for _logger in self._extra_loggers.values():
_logger.setLevel(level)
Expand Down Expand Up @@ -267,9 +269,11 @@ def get_logger(
return _logger


def _get_level(level: Optional[Union[str, int]] = None) -> str:
def _get_level(level: Optional[Union[str, int, LogLevel]] = None) -> str:
if level is None:
return DEFAULT_LOG_LEVEL
elif isinstance(level, LogLevel):
return level.name
elif isinstance(level, int) or level.isnumeric():
return LogLevel(int(level)).name

Expand Down
13 changes: 12 additions & 1 deletion tests/functional/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from ape.cli.choices import _NONE_NETWORK, _get_networks_sequence_from_cache
from ape.cli.commands import get_param_from_ctx, parse_network
from ape.exceptions import AccountsError
from ape.logging import logger
from ape.logging import LogLevel, logger
from tests.conftest import geth_process_test, skip_if_plugin_installed

OUTPUT_FORMAT = "__TEST__{0}:{1}:{2}_"
Expand Down Expand Up @@ -419,6 +419,17 @@ def cmd():
assert f"__expected_{logger.level}" in result.output


@pytest.mark.parametrize("level", (LogLevel.WARNING, LogLevel.WARNING.name, LogLevel.WARNING.value))
def test_verbosity_option_change_default(runner, level):
@click.command()
@verbosity_option(default=level)
def cmd():
pass

verbosity_parameter = cmd.params[0]
assert verbosity_parameter.default == level


def test_account_prompt_name():
"""
It is very important for this class to have the `name` attribute,
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from click.testing import CliRunner

from ape.cli import ape_cli_context
from ape.logging import LogLevel, logger


@pytest.fixture
Expand Down Expand Up @@ -98,3 +99,9 @@ def cmd(cli_ctx):

result = simple_runner.invoke(group_for_testing, ("cmd", "-v", "SUCCESS"))
assert "SUCCESS" not in result.output


@pytest.mark.parametrize("level", (LogLevel.INFO, LogLevel.INFO.value, LogLevel.INFO.name))
def test_set_level(level):
logger.set_level(level)
assert logger.level == LogLevel.INFO.value
Loading