-
Notifications
You must be signed in to change notification settings - Fork 711
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
Can't chain .opt() calls #192
Comments
Huh, this is a serious design issue indeed. Thanks for the report and the pull request. I'm not a big fan of sentinel values, as it makes the signature of the function less readable. I wonder if there is any other possible workaround... If not, I will probably merge your fix. 👍 |
Could switch to |
I don't think there's much point in chaining from functools import partialmethod
logger.__class__.foobar = partialmethod(logger.__class__.log, "foobar") I was delightfully surprised to find out that it worked just fine! I guess the Python interpreter somehow optimize |
I think that recipe change sounds like a good plan regardless of whether opt gets changed. from loguru import logger
logger = logger.opt(ansi=True)
try:
somethingdangerous()
except:
logger.opt(exception=True).debug('Everything <blue>went</> wrong') |
It seems possible to achieve the desired behavior using the same principle: from functools import partial
logger = logger.opt(ansi=True)
logger.opt = partial(logger.opt, ansi=True) Of course, this is not very intuitive, so this recipe needs to be properly documented. Nevertheless, I prefer this workaround. None of the solutions are perfect, so it's a matter of perspective, and I think it's more important to have a "clean" signature rather than allowing |
Sounds good, I'll defer to you on this one. Might also be worth a mention in the |
I updated the recipe for custom level, created a new entry about |
Awesome, looks good. Thanks! |
Do You have any way of working with |
@michalplat I think your problem relates to this There is even a comment mentioning the exact same problem but using the standard |
Thanks for Your input 😄 I think I'll just use pure |
I don't think I will be able to make any changes. Frankly I am quite busy nowadays. If you feel like you're up too, give it a shot. |
Calling opt more than once on a logger instance overrides all options that aren't specified to default, meaning you can't chain .opt() calls together.
For example in the following call, the color codes are ignored.
This doesn't make a lot of sense when done directly like this, but if you are using the recipe for adding custom loglevel methods, it calls .opt() to set the depth. The consequence of this is that you can no longer use .opt() directly from you code, since it will always be overridden by the depth call, including all of the options other than depth.
The text was updated successfully, but these errors were encountered: