-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
AttributeError raised when calling logger.exception without exception information #506
Comments
exc_info=True
when there were not exception raises an error
I indeed faced the same problem, trying to migrate from the standard lib to structlog. I instead used a custom processor, and changed I guess following the same behaviour as the standard library would be more intuitive, ans ease migration. def _figure_out_exc_info(v: Any) -> Union["sys._OptExcInfo", "ExcInfo"]:
if isinstance(v, BaseException):
return (v.__class__, v, v.__traceback__)
if isinstance(v, tuple):
return v # type: ignore[return-value]
if v:
return sys.exc_info() # type: ignore[return-value]
return (None, None, None)
def error_mapping_processor(_, __, event_dict: EventDict) -> EventDict:
if "exc_info" in event_dict:
exc_type, exc_value, exc_traceback = _figure_out_exc_info(event_dict.pop("exc_info"))
if exc_type is None or exc_value is None or exc_traceback is None:
return event_dict
event_dict["error.stack"] = "".join(traceback.format_exception(exc_type, exc_value, exc_traceback))
event_dict["error.message"] = str(exc_value)
event_dict["error.kind"] = exc_type.__name__
return event_dict |
@hynek Hello! What do you think about this issue?
|
So firstly I don't think structlog should crash here. I'm usually in the crash-early camp, but in this case the semantics are a bit muddy. That said, correct me if I'm wrong but ISTM that the problem are exception formatters that don't handle Therefore, the fix would be adding guard clauses before structlog/src/structlog/_frames.py Line 23 in d87abcb
structlog/src/structlog/dev.py Line 421 in d87abcb
What exactly would you suggest to test in |
@hynek hello!
What are your thoughts on this? |
Given that I think the correct place is indeed the one that gets the traceback (albeit indirect) and acts on it. Which would be the format processor. ConsoleRenderer too, because it does special magic for a better DX. As for the test I misread you. Of course we have to also add tests, once we add special treatment for this case. :) |
I believe #533 should fix your issues? |
Hello everyone!
Before I describe the issue, let me provide some context.
Main issue:
I encountered an error when attempting to call logger.exception outside an except block without including any information about an exception. For example:
This code raises the following error:
I find this error message confusing because it seems like an internal bug.
It is clear that moving the logger call inside the try/except block resolves the issue. Additionally, including information about the exception using constructions like
logger.exception('test', exc_info=False)
orlogger.exception('test', exc_info=ZeroDivisionError())
also prevents the error.The cause of the AttributeError is that the code blocks in
stdlib.BoundLogger.exception()
and_log_levels.exception()
set exc_info to True usingkw.setdefault("exc_info", True)
, even when there is no exc_info insys.exc_info()
. Subsequently, theprocessors._figure_out_exc_info()
function runs the following code:Because
sys.exc_info()
is blank we get tuple with value(None, None, None)
and then the following code raises AttributeError, that I showed above.Tests-related issue:
Furthermore, I noticed that some tests in the structlog library ignore this unexpected behavior. For instance, the tests TestBoundLogger.test_exception_exc_info() and TestFilteringLogger.test_exception() use BoundLogger(ReturnLogger(), [], {}) and do not check this case.
Solution
To address this issue, I would like to create a pull request to fix the problem. However, I would appreciate your advice on the following questions:
Thank you for your attention, and I look forward to your feedback.
The text was updated successfully, but these errors were encountered: