loguru icon indicating copy to clipboard operation
loguru copied to clipboard

Log have no color in docker logs

Open dogky123 opened this issue 1 year ago • 3 comments

image

from loguru import logger
logger.info(...)
logger.warning(...)

the script work in container but there is no color. All config is default.

thank you for your works.

dogky123 avatar Jul 12 '24 05:07 dogky123

By default, Loguru will try to automatically detect if the sink supports colors (e.g. console terminal as opposed to file sink) by calling the isatty() method.

The sys.stderr in your Docker container is likely redirected and not a tty, causing the colors to be disabled.

You can force them anyway by replacing the default sink with logger.add() and specifying colorize=True:

logger.remove()
logger.add(sys.stderr, colorize=True)

Delgan avatar Jul 20 '24 09:07 Delgan

For people using docker compose that don't want to modify the code, it's also possible to add an env var:

environment:
    LOGURU_COLORIZE: YES

Hugo-C avatar Sep 03 '25 08:09 Hugo-C

For people using docker compose that don't want to modify the code, it's also possible to add an env var:

environment:
    LOGURU_COLORIZE: YES

Using environment variable is a good solution. The downside of LOGURU_COLORIZE is that it'll also change the default behavior for handlers that generally do not support colors, such as a file sink for example. The next version of Loguru will support the FORCE_COLOR environment variable, which affects only terminal output, and should be preferred.

Delgan avatar Sep 03 '25 19:09 Delgan