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

Use real methods for fatal/warn #670

Merged
merged 1 commit into from
Nov 10, 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
22 changes: 17 additions & 5 deletions src/structlog/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ def critical(self, event: str | None = None, *args: Any, **kw: Any) -> Any:
"""
return self._proxy_to_logger("critical", event, *args, **kw)

def fatal(self, event: str | None = None, *args: Any, **kw: Any) -> Any:
"""
Process event and call `logging.Logger.critical` with the result.
"""
return self._proxy_to_logger("critical", event, *args, **kw)

def exception(
self, event: str | None = None, *args: Any, **kw: Any
) -> Any:
Expand All @@ -236,8 +242,6 @@ def log(
"""
return self._proxy_to_logger(LEVEL_TO_NAME[level], event, *args, **kw)

fatal = critical

def _proxy_to_logger(
self,
method_name: str,
Expand Down Expand Up @@ -448,7 +452,13 @@ async def acritical(self, event: str, *args: Any, **kw: Any) -> None:
"""
await self._dispatch_to_sync(self.critical, event, args, kw)

afatal = acritical
async def afatal(self, event: str, *args: Any, **kw: Any) -> None:
"""
Log using `critical()`, but asynchronously in a separate thread.

.. versionadded:: 23.1.0
"""
await self._dispatch_to_sync(self.critical, event, args, kw)

async def aexception(self, event: str, *args: Any, **kw: Any) -> None:
"""
Expand Down Expand Up @@ -625,15 +635,17 @@ async def info(self, event: str, *args: Any, **kw: Any) -> None:
async def warning(self, event: str, *args: Any, **kw: Any) -> None:
await self._dispatch_to_sync(self.sync_bl.warning, event, args, kw)

warn = warning
async def warn(self, event: str, *args: Any, **kw: Any) -> None:
await self._dispatch_to_sync(self.sync_bl.warning, event, args, kw)

async def error(self, event: str, *args: Any, **kw: Any) -> None:
await self._dispatch_to_sync(self.sync_bl.error, event, args, kw)

async def critical(self, event: str, *args: Any, **kw: Any) -> None:
await self._dispatch_to_sync(self.sync_bl.critical, event, args, kw)

fatal = critical
async def fatal(self, event: str, *args: Any, **kw: Any) -> None:
await self._dispatch_to_sync(self.sync_bl.critical, event, args, kw)

async def exception(self, event: str, *args: Any, **kw: Any) -> None:
# To make `log.exception("foo") work, we have to check if the user
Expand Down
35 changes: 33 additions & 2 deletions tests/test_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ def test_proxies_to_correct_method(self, method_name):

assert method_name == getattr(bl, method_name)("event")

def test_proxies_to_correct_method_special_cases(self):
"""
Fatal maps to critical and warn to warning.
"""
bl = BoundLogger(ReturnLogger(), [return_method_name], {})

assert "warning" == bl.warn("event")
assert "critical" == bl.fatal("event")

def test_proxies_log(self):
"""
BoundLogger.exception.log() is proxied to the appropriate method.
Expand Down Expand Up @@ -359,6 +368,20 @@ async def test_async_log_methods(self, meth, cl):
CapturedCall(method_name=meth, args=(), kwargs={"event": "Async!"})
] == cl.calls

async def test_async_log_methods_special_cases(self, cl):
"""
afatal maps to critical.
"""
bl = build_bl(cl, processors=[])

await bl.afatal("Async!")

assert [
CapturedCall(
method_name="critical", args=(), kwargs={"event": "Async!"}
)
] == cl.calls

async def test_alog(self, cl):
"""
Alog logs async at the correct level.
Expand Down Expand Up @@ -1325,11 +1348,19 @@ async def test_correct_levels(self, abl, cl, stdlib_log_method):

aliases = {"warn": "warning"}

alias = aliases.get(stdlib_log_method)
expect = alias if alias else stdlib_log_method
expect = aliases.get(stdlib_log_method, stdlib_log_method)

assert expect == cl.calls[0].method_name

@pytest.mark.asyncio
async def test_correct_level_fatal(self, abl, cl):
"""
fatal, that I have no idea why we support, maps to critical.
"""
await abl.bind(foo="bar").fatal("42")

assert "critical" == cl.calls[0].method_name

@pytest.mark.asyncio
async def test_log_method(self, abl, cl):
"""
Expand Down