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

mark lazy mode deprecated and add logger.lazy() for lazy format value #1233

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

trim21
Copy link
Contributor

@trim21 trim21 commented Nov 12, 2024

No description provided.

@trim21
Copy link
Contributor Author

trim21 commented Nov 12, 2024

ci failed at codecov, not test/lint

@Delgan
Copy link
Owner

Delgan commented Nov 20, 2024

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 LazyValue class just to optimize lazy evaluations of message formatting. So far, the logger has been the only publicly importable component from loguru. That would be a significant change of this idiom for what seems to be a relatively small inconvenience.

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.

@trim21
Copy link
Contributor Author

trim21 commented Nov 20, 2024

maybe we can add a api logger.lazy(fn, *args, **kwargs)?

I don't think PEP 750 will help #782 or this, even with pep750 accepted, value in template str is still calculcated before logger function is called.

@trim21
Copy link
Contributor Author

trim21 commented Nov 20, 2024

also we have only sync api, I don't know there is a method to handle #782

@Delgan
Copy link
Owner

Delgan commented Nov 20, 2024

value in template str is still calculcated before logger function is called.

Indeed, but it's possible to pass a lambda: Approaches to Lazy Evaluation

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 opt(lazy=true) argument obsolete, while making it straightforward to combine with non-lazy arguments.

This PEP is actively discussed here.

maybe we can add a api logger.lazy(fn, *args, **kwargs)?

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.

also we have only sync api, I don't know there is a method to handle #782

Yes, unfortunately I don't know how to integrate it either.

@trim21
Copy link
Contributor Author

trim21 commented Nov 20, 2024

We could imagine than when a template string is used, then Loguru will convert callable arguments, but only if the log level requires it.

personal I think this is confusing and dangerous behaviour to call callable in arguments unless explicit specified with lazy or something

@trim21 trim21 changed the title mark lazy mode is deprecated and add LazyValue for lazy format value mark lazy mode deprecated and add LazyValue for lazy format value Nov 25, 2024
@trim21
Copy link
Contributor Author

trim21 commented Nov 25, 2024

what do you think about a new public API loguru.utils.LazyValue/Lazy/lazy instead of loguru.LazyValue?

even with PEP 750 accepted I think the api should be logger.debug(t'hello {Lazy(fn, ...)}') or logger.debug(t'hello {logger.lazy(fn, ...)}')

@trim21 trim21 changed the title mark lazy mode deprecated and add LazyValue for lazy format value mark lazy mode deprecated and add logger.lazy() for lazy format value Nov 25, 2024
@Delgan
Copy link
Owner

Delgan commented Nov 26, 2024

Thanks for the update.

Sorry, I should have catch that earlier but I realized there are two mains problems with such API:

  • The expensive function will be called multiple time if the argument is to be formatted multiple times (e.g. logger.info("{foo} {foo}", foo=logger.lazy(func))).
  • The extra dict which be populated with a LazyValue instead of the underlying value (for structured logging).

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)

@trim21
Copy link
Contributor Author

trim21 commented Nov 26, 2024

I agree we should have a method for isEnabledFor.

These problems are easy to fix, we can just cache result and modify json encoder's default argument we used

{"text": "2024-11-27 03:32:58.508 | INFO     | __main__:<module>:13 - 1 1\n", "record": {"elapsed": {"repr": "0:00:00.002001", "seconds": 0.002001}, "exception": null, "extra": {"foo": "1"}, "file": {"name": "a.py", "path": "C:\\Users\\Trim21\\proj\\loguru\\a.py"}, "function": "<module>", "level": {"icon": "ℹ️", "name": "INFO", "no": 20}, "line": 13, "message": "1 1", "module": "a", "name": "__main__", "process": {"id": 7940, "name": "MainProcess"}, "thread": {"id": 27388, "name": "MainThread"}, "time": {"repr": "2024-11-27 03:32:58.508597+08:00", "timestamp": 1732649578.508597}}}
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)

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

Successfully merging this pull request may close these issues.

2 participants