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

Log Files Displaying Bad Format and Colors #1100

Open
shlomi93 opened this issue Mar 4, 2024 · 2 comments
Open

Log Files Displaying Bad Format and Colors #1100

shlomi93 opened this issue Mar 4, 2024 · 2 comments
Labels
question Further information is requested

Comments

@shlomi93
Copy link

shlomi93 commented Mar 4, 2024

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)
@smuglik
Copy link

smuglik commented Mar 20, 2024

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

@Delgan
Copy link
Owner

Delgan commented Mar 30, 2024

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.

@Delgan Delgan added the question Further information is requested label Mar 30, 2024
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

3 participants