-
Notifications
You must be signed in to change notification settings - Fork 703
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
mark lazy mode deprecated and add logger.lazy()
for lazy format value
#1233
base: master
Are you sure you want to change the base?
Conversation
ci failed at codecov, not test/lint |
Hi @trim21, thanks for opening a PR. I understand it refers to #1207. I've been thinking about this lately, and to be honest I'm not very keen on the idea adding a new I would welcome a new API for lazy evaluated arguments if it also address #782. However, this is not necessarily easy. Another thing to consider is PEP 750. It's still draft and there's no guarantee that it will be accepted, but it could be interesting for our use case, without requiring a new API. |
also we have only sync api, I don't know there is a method to handle #782 |
Indeed, but it's possible to pass a We could imagine than when a template string is used, then Loguru will convert callable arguments, but only if the log level requires it. This would make the This PEP is actively discussed here.
I would prefer such API, yes. But still, that's a new method that increases the complexity of Loguru's API, for a marginal gain in my opinion.
Yes, unfortunately I don't know how to integrate it either. |
personal I think this is confusing and dangerous behaviour to call callable in arguments unless explicit specified with lazy or something |
LazyValue
for lazy format valueLazyValue
for lazy format value
what do you think about a new public API even with PEP 750 accepted I think the api should be |
LazyValue
for lazy format valuelogger.lazy()
for lazy format value
Thanks for the update. Sorry, I should have catch that earlier but I realized there are two mains problems with such API:
Even if we managed to get around these problems, to be honest I'm still not convinced by such an API change. We're trying to fix a mere inconvenience. But there are other patterns that are not possible today and that it would be more interesting to handle. Notably #782 as said, or even: # There is no equivalent possible in Loguru.
if logger.isEnabledFor(logging.DEBUG):
foo = expensive_func()
logger.info("Foo: %s", foo.summary)
logger.debug("Foo details: %s", foo.details) |
I agree we should have a method for These problems are easy to fix, we can just cache result and modify json encoder's default argument we used
class LazyValue:
def __init__(self, fn, *args, **kwargs):
self.fn = fn
self.args = args
self.kwargs = kwargs
def __format__(self, format_spec: str):
return format(self.__result, format_spec)
def __str__(self):
return str(self.__result)
def __repr__(self):
return repr(self.__result)
@functools.cached_property
def __result(self):
return self.fn(*self.args, **self.kwargs) |
No description provided.