Python retry package - tenacity : How to log the root cause of exception?












1















As discussed in this question I am using tenacity to do retries.



A toy code looks like below



import logging
from tenacity import retry
import tenacity


@retry(wait=tenacity.wait_incrementing(start=10, increment=10, max=100), stop=tenacity.stop_after_attempt(3))
def print_msg():
logging.info('Hello')
logging.info("World")
raise Exception('Test error')


if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%d-%m-%Y:%H:%M:%S',
level=logging.INFO)
logging.info('Starting')
print_msg()


The output looks like below



21-11-2018:12:40:48,586 INFO     [retrier.py:18] Starting
21-11-2018:12:40:48,586 INFO [retrier.py:8] Hello
21-11-2018:12:40:48,586 INFO [retrier.py:9] World
21-11-2018:12:40:58,592 INFO [retrier.py:8] Hello
21-11-2018:12:40:58,592 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 INFO [retrier.py:8] Hello
21-11-2018:12:41:18,596 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 ERROR [retrier.py:22] Received Exception
Traceback (most recent call last):
File "/Users/dmanna/PycharmProjects/demo/retrier.py", line 20, in <module>
print_msg()
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 292, in wrapped_f
return self.call(f, *args, **kw)
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 358, in call
do = self.iter(retry_state=retry_state)
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 332, in iter
six.raise_from(retry_exc, fut.exception())
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/six.py", line 737, in raise_from
raise value
RetryError: RetryError[<Future at 0x109fa6150 state=finished raised Exception>]


Can someone let me know how can I log the root cause of the exception?










share|improve this question























  • you could see the whole exception chain under python3.

    – georgexsh
    Nov 21 '18 at 8:13
















1















As discussed in this question I am using tenacity to do retries.



A toy code looks like below



import logging
from tenacity import retry
import tenacity


@retry(wait=tenacity.wait_incrementing(start=10, increment=10, max=100), stop=tenacity.stop_after_attempt(3))
def print_msg():
logging.info('Hello')
logging.info("World")
raise Exception('Test error')


if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%d-%m-%Y:%H:%M:%S',
level=logging.INFO)
logging.info('Starting')
print_msg()


The output looks like below



21-11-2018:12:40:48,586 INFO     [retrier.py:18] Starting
21-11-2018:12:40:48,586 INFO [retrier.py:8] Hello
21-11-2018:12:40:48,586 INFO [retrier.py:9] World
21-11-2018:12:40:58,592 INFO [retrier.py:8] Hello
21-11-2018:12:40:58,592 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 INFO [retrier.py:8] Hello
21-11-2018:12:41:18,596 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 ERROR [retrier.py:22] Received Exception
Traceback (most recent call last):
File "/Users/dmanna/PycharmProjects/demo/retrier.py", line 20, in <module>
print_msg()
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 292, in wrapped_f
return self.call(f, *args, **kw)
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 358, in call
do = self.iter(retry_state=retry_state)
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 332, in iter
six.raise_from(retry_exc, fut.exception())
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/six.py", line 737, in raise_from
raise value
RetryError: RetryError[<Future at 0x109fa6150 state=finished raised Exception>]


Can someone let me know how can I log the root cause of the exception?










share|improve this question























  • you could see the whole exception chain under python3.

    – georgexsh
    Nov 21 '18 at 8:13














1












1








1








As discussed in this question I am using tenacity to do retries.



A toy code looks like below



import logging
from tenacity import retry
import tenacity


@retry(wait=tenacity.wait_incrementing(start=10, increment=10, max=100), stop=tenacity.stop_after_attempt(3))
def print_msg():
logging.info('Hello')
logging.info("World")
raise Exception('Test error')


if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%d-%m-%Y:%H:%M:%S',
level=logging.INFO)
logging.info('Starting')
print_msg()


The output looks like below



21-11-2018:12:40:48,586 INFO     [retrier.py:18] Starting
21-11-2018:12:40:48,586 INFO [retrier.py:8] Hello
21-11-2018:12:40:48,586 INFO [retrier.py:9] World
21-11-2018:12:40:58,592 INFO [retrier.py:8] Hello
21-11-2018:12:40:58,592 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 INFO [retrier.py:8] Hello
21-11-2018:12:41:18,596 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 ERROR [retrier.py:22] Received Exception
Traceback (most recent call last):
File "/Users/dmanna/PycharmProjects/demo/retrier.py", line 20, in <module>
print_msg()
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 292, in wrapped_f
return self.call(f, *args, **kw)
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 358, in call
do = self.iter(retry_state=retry_state)
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 332, in iter
six.raise_from(retry_exc, fut.exception())
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/six.py", line 737, in raise_from
raise value
RetryError: RetryError[<Future at 0x109fa6150 state=finished raised Exception>]


Can someone let me know how can I log the root cause of the exception?










share|improve this question














As discussed in this question I am using tenacity to do retries.



A toy code looks like below



import logging
from tenacity import retry
import tenacity


@retry(wait=tenacity.wait_incrementing(start=10, increment=10, max=100), stop=tenacity.stop_after_attempt(3))
def print_msg():
logging.info('Hello')
logging.info("World")
raise Exception('Test error')


if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%d-%m-%Y:%H:%M:%S',
level=logging.INFO)
logging.info('Starting')
print_msg()


The output looks like below



21-11-2018:12:40:48,586 INFO     [retrier.py:18] Starting
21-11-2018:12:40:48,586 INFO [retrier.py:8] Hello
21-11-2018:12:40:48,586 INFO [retrier.py:9] World
21-11-2018:12:40:58,592 INFO [retrier.py:8] Hello
21-11-2018:12:40:58,592 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 INFO [retrier.py:8] Hello
21-11-2018:12:41:18,596 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 ERROR [retrier.py:22] Received Exception
Traceback (most recent call last):
File "/Users/dmanna/PycharmProjects/demo/retrier.py", line 20, in <module>
print_msg()
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 292, in wrapped_f
return self.call(f, *args, **kw)
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 358, in call
do = self.iter(retry_state=retry_state)
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/tenacity/__init__.py", line 332, in iter
six.raise_from(retry_exc, fut.exception())
File "/Users/dmanna/PycharmProjects/demo/venv/lib/python2.7/site-packages/six.py", line 737, in raise_from
raise value
RetryError: RetryError[<Future at 0x109fa6150 state=finished raised Exception>]


Can someone let me know how can I log the root cause of the exception?







python python-2.7






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 7:13









tuktuk

1,59522354




1,59522354













  • you could see the whole exception chain under python3.

    – georgexsh
    Nov 21 '18 at 8:13



















  • you could see the whole exception chain under python3.

    – georgexsh
    Nov 21 '18 at 8:13

















you could see the whole exception chain under python3.

– georgexsh
Nov 21 '18 at 8:13





you could see the whole exception chain under python3.

– georgexsh
Nov 21 '18 at 8:13












1 Answer
1






active

oldest

votes


















2














you could use reraise=True option, there is no verbose doc, but you could read its source:



...
File "53406953.py", line 10, in print_msg
raise Exception('Test error')
Exception: Test error





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406953%2fpython-retry-package-tenacity-how-to-log-the-root-cause-of-exception%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    you could use reraise=True option, there is no verbose doc, but you could read its source:



    ...
    File "53406953.py", line 10, in print_msg
    raise Exception('Test error')
    Exception: Test error





    share|improve this answer






























      2














      you could use reraise=True option, there is no verbose doc, but you could read its source:



      ...
      File "53406953.py", line 10, in print_msg
      raise Exception('Test error')
      Exception: Test error





      share|improve this answer




























        2












        2








        2







        you could use reraise=True option, there is no verbose doc, but you could read its source:



        ...
        File "53406953.py", line 10, in print_msg
        raise Exception('Test error')
        Exception: Test error





        share|improve this answer















        you could use reraise=True option, there is no verbose doc, but you could read its source:



        ...
        File "53406953.py", line 10, in print_msg
        raise Exception('Test error')
        Exception: Test error






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 8:26

























        answered Nov 21 '18 at 8:20









        georgexshgeorgexsh

        10.8k11338




        10.8k11338
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406953%2fpython-retry-package-tenacity-how-to-log-the-root-cause-of-exception%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()