Delgan / loguru

Python logging made (stupidly) simple

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Log Files Displaying Bad Format and Colors

shlomi93 opened this issue · comments

Hi,
When I use the logger I can see the logs formatted and colored in the console (PyCharm)
But, when opening the actual file, I get unformatted raw ASCII. See pic below.
How can I fix this? Tried to open in many different txt editors, all seem to get the same result, including opening in PyCharm itself.
MacOS 14.3.1

In PyCharm console (during runtime or debugging)
image

When opening the actual file:
image

code:

if __name__ == '__main__':
    logger.add("my_logger.log", rotation="03:30", colorize=True,
               format="<green>{time}</green> <level>{message}</level>")
    logger.info("logging some info")
    logger.debug("logging some debug stuff")
    logger.error("logging error")
    exit(1)

Hi @shlomi93 ,
I am not sure, but colorize=True is not really needed. I've set it to False and it works as you want.

Here is a code:

from loguru import logger

if __name__ == '__main__':
    logger.add("my_logger.log", rotation="03:30", colorize=False,
               format="<green>{time}</green> <level>{message}</level>")
    logger.info("logging some info")
    logger.debug("logging some debug stuff")
    logger.error("logging error")

Log file:

2024-03-20T16:30:30.973518+0300 logging some info
2024-03-20T16:30:30.974060+0300 logging some debug stuff
2024-03-20T16:30:30.974191+0300 logging error

Hi @shlomi93.

As indicated by @smuglik (thanks), the colorize=True makes little sense with log files. This is because the colored logs on your terminal are implemented using ANSI escape sequences, but these color codes are not intended for files.

The files does not conventionally support ANSI codes. There is no way to natively colorize the content of a file. If you want to see colors, the ANSI code would need to be reinterpreted and converted somehow, for example using a VS Code extension: ANSI Colors.