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: @ape.logging.silenced decorator for ensuring an entire method does not log #2495

Merged
merged 2 commits into from
Feb 7, 2025
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
17 changes: 16 additions & 1 deletion src/ape/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,19 @@ def get_rich_console(file: Optional[IO[str]] = None, **kwargs) -> "RichConsole":
return _factory.get_console(file, **kwargs)


__all__ = ["DEFAULT_LOG_LEVEL", "logger", "LogLevel", "ApeLogger", "get_rich_console"]
def silenced(func: Callable):
"""
A decorator for ensuring a function does not output any logs.

Args:
func (Callable): The function to call silently.
"""

def wrapper(*args, **kwargs):
with logger.disabled():
return func(*args, **kwargs)

return wrapper


__all__ = ["DEFAULT_LOG_LEVEL", "logger", "LogLevel", "ApeLogger", "get_rich_console", "silenced"]
13 changes: 12 additions & 1 deletion tests/functional/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from click.testing import CliRunner

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


@pytest.fixture
Expand Down Expand Up @@ -147,3 +147,14 @@ def test_disabled(ape_caplog):
# Show it is back.
logger.error(message)
assert message in ape_caplog.head


def test_silenced(ape_caplog):
@silenced
def method_to_silence(x: int, y: str):
logger.error(f"{x} {y}")

magic = [156236, "Asd#g"]
unexpected = f"{magic[0]} {magic[1]}"
method_to_silence(*magic)
assert unexpected not in ape_caplog.head
Loading