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 for async task and catch __all__ from module. #801

Closed
PieceOfGood opened this issue Feb 10, 2023 · 2 comments
Closed

@logger.catch for async task and catch __all__ from module. #801

PieceOfGood opened this issue Feb 10, 2023 · 2 comments
Labels
question Further information is requested

Comments

@PieceOfGood
Copy link

I would love to have comments on this post.

It is also interesting to know the following: if it is not possible to catch exceptions with the @logger.catch decorator for asynchronous functions used only as tasks, except by explicitly specifying this decorator above the declaration of each such function, then is there any possibility, say, to declare isolate a set of such functions in a separate module through __all__ = [], and then, when importing into __init__.py, loop through them and force them to be decorated? For example:

# __init__.py
from loguru import logger
import module

for name in module.__all__:
    func = getattr(module, name)
    wrapped = logger.catch_wrapper(func)
    setattr(module, name, wrapped)

__all__ = [ "module" ]
@PieceOfGood
Copy link
Author

Understood the last one.

# __init__.py
from loguru import logger
import module

for name in module.__all__:
    func = getattr(module, name)
    wrapped = logger.catch()(func)
    setattr(module, name, wrapped)

__all__ = [ "module" ]

@Delgan Delgan added the question Further information is requested label Feb 15, 2023
@Delgan
Copy link
Owner

Delgan commented Feb 15, 2023

Sorry for not answering sooner.

I would certainly not advise to blindly decorate all variables in __all__, but you can indeed use logger.catch() this way (like any other decorator in Python).
I would say it's preferable to only wrap your main() function or entry point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants