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

Allow positional args in exception #531

Merged
merged 1 commit into from
Jul 22, 2023
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
12 changes: 8 additions & 4 deletions src/structlog/_log_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ async def _anop(self: Any, event: str, *args: Any, **kw: Any) -> Any:
return None


def exception(self: FilteringBoundLogger, event: str, **kw: Any) -> Any:
def exception(
self: FilteringBoundLogger, event: str, *args: Any, **kw: Any
) -> Any:
kw.setdefault("exc_info", True)

return self.error(event, **kw)
return self.error(event, *args, **kw)


async def aexception(self: FilteringBoundLogger, event: str, **kw: Any) -> Any:
async def aexception(
self: FilteringBoundLogger, event: str, *args: Any, **kw: Any
) -> Any:
# Exception info has to be extracted this early, because it is no longer
# available once control is passed to the executor.
if kw.get("exc_info", True) is True:
Expand All @@ -95,7 +99,7 @@ async def aexception(self: FilteringBoundLogger, event: str, **kw: Any) -> Any:
ctx = contextvars.copy_context()
return await asyncio.get_running_loop().run_in_executor(
None,
lambda: ctx.run(lambda: self.error(event, **kw)),
lambda: ctx.run(lambda: self.error(event, *args, **kw)),
)


Expand Down
18 changes: 18 additions & 0 deletions tests/test_log_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ async def test_async_exception(self, bl, cl):
assert isinstance(cl.calls[0][2]["exc_info"], tuple)
assert exc == cl.calls[0][2]["exc_info"][1]

def test_exception_positional_args(self, bl, cl):
"""
exception allows for positional args
"""
bl.exception("%s %s", "boom", "bastic")

assert [
("error", (), {"event": "boom bastic", "exc_info": True})
] == cl.calls

async def test_aexception_positional_args(self, bl, cl):
"""
aexception allows for positional args
"""
await bl.aexception("%s %s", "boom", "bastic")
assert 1 == len(cl.calls)
assert "boom bastic" == cl.calls[0][2]["event"]

async def test_async_exception_true(self, bl, cl):
"""
aexception replaces exc_info with current exception info, if exc_info
Expand Down