loguru icon indicating copy to clipboard operation
loguru copied to clipboard

Lazy colours

Open astromancer opened this issue 3 years ago • 1 comments

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.

astromancer avatar Jul 06 '22 10:07 astromancer

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 avatar Jul 07 '22 20:07 Delgan