tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

Before sleep log stack info

Open jpark712 opened this issue 4 years ago • 3 comments

This change adds the stack_info parameter to the before_sleep_log() function and passes it to the logger function. When set to True, logger will include a stack dump at the current location, which can be useful to pinpointing the exact location in code that triggered a retry.

Closes #290

jpark712 avatar Apr 15 '21 17:04 jpark712

Example of before_sleep_log called with exc_info=True, stack_info=False:

test_func called()
INFO:__main__:Retrying __main__.test_func in 1.0 seconds as it raised Exception: my error.
Traceback (most recent call last):
  File "C:\github\forks\tenacity\tenacity\__init__.py", line 433, in __call__
    result = fn(*args, **kwargs)
  File ".\ten.py", line 35, in test_func
    raise Exception("my error")
Exception: my error

Example of before_sleep_log called with exc_info=True, stack_info=True. Notice we can now see that test_func was called from line 117 from the main() method whereas before, we only get the line that raised the exception.

test_func called()
INFO:__main__:Retrying __main__.test_func in 1.0 seconds as it raised Exception: my error.
Traceback (most recent call last):
  File "C:\github\forks\tenacity\tenacity\__init__.py", line 433, in __call__
    result = fn(*args, **kwargs)
  File ".\ten.py", line 35, in test_func
    raise Exception("my error")
Exception: my error
Stack (most recent call last):
  File ".\ten.py", line 117, in <module>
    main()
  File ".\ten.py", line 94, in main
    r.call(test_func, 1, 2)
  File "C:\github\forks\tenacity\tenacity\__init__.py", line 419, in call
    return self.__call__(*args, **kwargs)
  File "C:\github\forks\tenacity\tenacity\__init__.py", line 430, in __call__
    do = self.iter(retry_state=retry_state)
  File "C:\github\forks\tenacity\tenacity\__init__.py", line 391, in iter
    self.before_sleep(retry_state=retry_state)
  File "C:\github\forks\tenacity\tenacity\before_sleep.py", line 41, in log_it
    logger.log(

jpark712 avatar Apr 15 '21 20:04 jpark712

Ah I think CI is broken again. Really need to move to GH Actions.

jd avatar Apr 26 '21 13:04 jd

This would be super useful to have. Is there any chance of landing this soon?

RoyalTS avatar Jul 06 '21 08:07 RoyalTS

Old and in conflict, closing.

jd avatar Aug 21 '23 07:08 jd