Skip to content

Commit

Permalink
feat: @ape.logging.silenced decorator for ensuring an entire method…
Browse files Browse the repository at this point in the history
… does not log (#2495)
  • Loading branch information
antazoey authored Feb 7, 2025
1 parent a35ba6d commit c8022fe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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

0 comments on commit c8022fe

Please sign in to comment.