Skip to content
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

logger.catch decorator doesn't print pretty traceback on async functions #75

Closed
Fak3 opened this issue Mar 30, 2019 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@Fak3
Copy link

Fak3 commented Mar 30, 2019

loguru v0.2.5
Minimal example:

from loguru import logger

@logger.catch
async def xxx():
    x = 3 / 0
        
asyncio.run(xxx())

Prints just plain traceback:

Traceback (most recent call last):
  File "./download.py", line 201, in <module>
    asyncio.run(xxx())
  File "/usr/lib64/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/lib64/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "./download.py", line 199, in xxx
    x = 3 / 0
ZeroDivisionError: division by zero

But if i catch the exception and log it from the function, i get pretty traceback, including values of variables:

from loguru import logger

async def xxx():
    zero = 0
    try:
        x = 3 / zero
    except:
        logger.exception('boom')
        
asyncio.run(xxx())

Prints:

2019-03-30 18:47:43.563 | ERROR    | __main__:xxx:202 - boom
Traceback (most recent call last):

  File "./download.py", line 204, in <module>
    asyncio.run(xxx())
    │       │   └ <function xxx at 0x7f7547453ea0>
    │       └ <function run at 0x7f7547a4bb70>
    └ <module 'asyncio' from '/usr/lib64/python3.7/asyncio/__init__.py'>

  File "/usr/lib64/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
           │    │                  └ <coroutine object xxx at 0x7f7547e33ec8>
           │    └ <bound method BaseEventLoop.run_until_complete of <_UnixSelectorEventLoop running=True closed=False debug=False>>
           └ <_UnixSelectorEventLoop running=True closed=False debug=False>
  File "/usr/lib64/python3.7/asyncio/base_events.py", line 571, in run_until_complete
    self.run_forever()
    │    └ <bound method BaseEventLoop.run_forever of <_UnixSelectorEventLoop running=True closed=False debug=False>>
    └ <_UnixSelectorEventLoop running=True closed=False debug=False>
  File "/usr/lib64/python3.7/asyncio/base_events.py", line 539, in run_forever
    self._run_once()
    │    └ <bound method BaseEventLoop._run_once of <_UnixSelectorEventLoop running=True closed=False debug=False>>
    └ <_UnixSelectorEventLoop running=True closed=False debug=False>
  File "/usr/lib64/python3.7/asyncio/base_events.py", line 1775, in _run_once
    handle._run()
    │      └ <bound method Handle._run of <Handle <TaskStepMethWrapper object at 0x7f75474516d8>()>>
    └ <Handle <TaskStepMethWrapper object at 0x7f75474516d8>()>
  File "/usr/lib64/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
    │    │        │   │    │           │    └ ()
    │    │        │   │    │           └ <Handle <TaskStepMethWrapper object at 0x7f75474516d8>()>
    │    │        │   │    └ <TaskStepMethWrapper object at 0x7f75474516d8>
    │    │        │   └ <Handle <TaskStepMethWrapper object at 0x7f75474516d8>()>
    │    │        └ <built-in method run of Context object at 0x7f75473d75e8>
    │    └ <Context object at 0x7f75473d75e8>
    └ <Handle <TaskStepMethWrapper object at 0x7f75474516d8>()>

> File "./download.py", line 200, in xxx
    x = 3 / zero
            └ 0

ZeroDivisionError: division by zero

@Delgan
Copy link
Owner

Delgan commented Mar 30, 2019

Thanks for the bug report. 😉

I admit I never tried to decorate a coroutine. Today I learned that to wrap an async function, well, the wrapper function need to be async too.

This should be easily fixed.

@Delgan Delgan added the bug Something isn't working label Mar 30, 2019
@Delgan
Copy link
Owner

Delgan commented Apr 6, 2019

This will be fixed in v0.3.0.

I also added support for decorating generator functions. It should also be possible to decorate asynchronous generators, but I prefer to better understand all the async / await stuff before implementing it.

@Delgan Delgan closed this as completed Apr 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants