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

Lazy colours #675

Closed
astromancer opened this issue Jul 6, 2022 · 1 comment
Closed

Lazy colours #675

astromancer opened this issue Jul 6, 2022 · 1 comment
Labels
question Further information is requested

Comments

@astromancer
Copy link

Is is possible to have colours defined via markup syntax evaluate lazily?
For example:

from loguru import logger

logger.opt(colors=True, lazy=True).debug('<g>Hello</> {}', lambda: '<red>World!</>')

Currently this only resolves the colours for the static part of the message.

@Delgan
Copy link
Owner

Delgan commented Jul 7, 2022

This functionality was removed some time ago because it caused unexpected errors when the logging arguments unintentionally contained markup tags, see #197.

A possibly workaround is to generate your message and use the logger inside as lazy function, as follow:

from loguru import logger

def lazy():
    lazily_generated_message = "<g>Hello</> <red>World</>"
    logger.opt(colors=True).debug(lazily_generated_message)

logger.opt(raw=True, lazy=True).debug("", lazy)

This might became possible again in the future if I add a method allowing user to fully handle message formatting, I keep your use case in mind.

@Delgan Delgan added the question Further information is requested label Oct 7, 2022
@Delgan Delgan closed this as completed Oct 7, 2022
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