You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are using structlog for creating canonical logs in a lambda function. The entrypoint of the lambda function calls another method which handles the event. This method never raises, but code within the other method might trap an exception and place it in the contextvar key exc_info .
In production, we log output as json and everything is fine. For local development, we are using the console renderer, but the exceptions are not handled properly unless the exc_key contains a tuple.
Here is a minimal reproduction:
importstructlogfromstructlog.contextvarsimportbind_contextvarslogger=structlog.stdlib.get_logger()
defprocess():
bind_contextvars(event_id="123")
# do some things, but don't propagate the exceptiontry:
print("Running...")
raiseValueError("Boom")
exceptExceptionasexc:
bind_contextvars(exc_info=exc)
print("Finished...")
defrun():
try:
process()
finally:
logger.info("canonical-log")
if__name__=="__main__":
run()
When running, this explodes while handling the log:
$ poetry run python3 main.py
Running...
Finished...
Traceback (most recent call last):
File "/Users/aclemons/worksp/structlog-test/main.py", line 25, in<module>run()
File "/Users/aclemons/worksp/structlog-test/main.py", line 22, in run
logger.info("canonical-log")
File "/Users/aclemons/Library/Caches/pypoetry/virtualenvs/structlog-test-wfc9biWa-py3.9/lib/python3.9/site-packages/structlog/_log_levels.py", line 157, in meth
return self._proxy_to_logger(name, event, **kw)
File "/Users/aclemons/Library/Caches/pypoetry/virtualenvs/structlog-test-wfc9biWa-py3.9/lib/python3.9/site-packages/structlog/_base.py", line 205, in _proxy_to_logger
args, kw = self._process_event(method_name, event, event_kw)
File "/Users/aclemons/Library/Caches/pypoetry/virtualenvs/structlog-test-wfc9biWa-py3.9/lib/python3.9/site-packages/structlog/_base.py", line 162, in _process_event
event_dict = proc(self._logger, method_name, event_dict)
File "/Users/aclemons/Library/Caches/pypoetry/virtualenvs/structlog-test-wfc9biWa-py3.9/lib/python3.9/site-packages/structlog/dev.py", line 424, in __call__
self._exception_formatter(sio, exc_info)
File "/Users/aclemons/Library/Caches/pypoetry/virtualenvs/structlog-test-wfc9biWa-py3.9/lib/python3.9/site-packages/structlog/dev.py", line 187, in rich_traceback
Traceback.from_exception(*exc_info, show_locals=True)
File "/Users/aclemons/Library/Caches/pypoetry/virtualenvs/structlog-test-wfc9biWa-py3.9/lib/python3.9/site-packages/rich/traceback.py", line 292, in from_exception
rich_traceback = cls.extract(
File "/Users/aclemons/Library/Caches/pypoetry/virtualenvs/structlog-test-wfc9biWa-py3.9/lib/python3.9/site-packages/rich/traceback.py", line 348, in extract
exc_type=safe_str(exc_type.__name__),
AttributeError: 'NoneType' object has no attribute '__name__'
If the exception is propagated it works without problem.
First, thank you for this great package.
We are using structlog for creating canonical logs in a lambda function. The entrypoint of the lambda function calls another method which handles the event. This method never raises, but code within the other method might trap an exception and place it in the contextvar key exc_info .
In production, we log output as json and everything is fine. For local development, we are using the console renderer, but the exceptions are not handled properly unless the exc_key contains a tuple.
Here is a minimal reproduction:
When running, this explodes while handling the log:
If the exception is propagated it works without problem.
It looks like the problem is here:
https://github.com/hynek/structlog/blob/22.3.0/src/structlog/dev.py#L420-L424
Since exc_info is not a tuple, it calls sys.exc_info, but we are not currently handling an exception and this returns None values.
Would it be appropriate to replace that with the following code?
That is what
ExceptionRenderer
is using here.This then works for me.
The text was updated successfully, but these errors were encountered: