Skip to content

Commit

Permalink
Allow positional args in exception (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcouper committed Jul 22, 2023
1 parent ec810c5 commit ea950f6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
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

0 comments on commit ea950f6

Please sign in to comment.