loguru
loguru copied to clipboard
Lazy colours
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.
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.