Logging the exception calls again the function in which it happened
See original GitHub issueMight be related to #88.
I noticed this issue because my tests (ran with pytest
) were breaking when I added a fixture to enable the logger.
Here is some minimal example. I added comments to explain what seems to happen.
class Whatever:
@property
def following(self):
if not self._following: # self._following initialized to None in __init__
following_id = "some id"
try:
self._following = do_something_with(following_id)
except SomeException as error: # triggered
logger.warning(f"blah blah {following_id}, blah blah blah ({id(self)}") # this works fine
logger.opt(exception=True).trace(error) # this re-enter this very property
self._following = None # this code is never reached
return self._following # this code is never reached
In the generated log file, I then have 91 lines with the same warning. I tried adding depth=-1
in the opt()
parameters, but it changes nothing. The instance of SomeException
does not contain any reference to the Whatever
instance.
- If I remove the
.opt(exception=True)
it works fine again (but I get no traceback) - If I pass the exception directly with
logger.opt(exception=error).trace(error)
it works as well, but it does not print the traceback. - If I pass the exception info, with
logger.opt(exception=sys.exc_info()).trace(error)
, it does not work. - If I only retrieve the exception info with
exc_info = sys.exc_info()
, without logging it, it works fine (so the issue must come from what loguru does with the exception).
I guess the following
property is triggered when inspecting sys.exc_info
or something? Is there anything loguru can do about this? If not, I can still pass the exception directly so it’s not so bad. It would be interesting to replicate this in a test though.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Logging Exceptions in Java - Loggly
Logging can take place in the method where the exception occurred, or it can follow the exception up the call stack until it's...
Read more >What happens if a python logging handler raises an exception?
If logging.raiseExceptions has been set to False , all handlers will be called, even if one of them fails with an exception.
Read more >Does the responsibility of logging method/function calls fall to ...
The callee is not responsible for logging in the sense that it is not handling/implementing it, it is just using it. So regarding...
Read more >How to log a Python exception? - GeeksforGeeks
Logging an exception in python with an error can be done in the logging.exception() method. This function logs a message with level ERROR...
Read more >9 Best Practices to Handle Java Exceptions - Stackify
An exception happens when something goes wrong. Tried to open a file but it doesn't exist? You've got an exception. Attempted to call...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I tested it and it failed just as expected, while it worked fine with the latest Loguru version. I will update it adequately if needed, thanks again. 👍
No problem! I didn’t try the test code though ⚠️ ! You might need to update it!