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

"[33m[1" instead of colored text #246

Closed
bataliero opened this issue Apr 14, 2020 · 3 comments
Closed

"[33m[1" instead of colored text #246

bataliero opened this issue Apr 14, 2020 · 3 comments
Labels
question Further information is requested

Comments

@bataliero
Copy link

Hi, I try to integrate loguru to a project containing logging from std library. Unfortunately I got messages prepended with [33m[1 characters when script is invoked by cmd.exe or Windows PowerShell. Interestingly git-bash (mintty.exe) works good and output is colored as expected! Example below:

import loguru

old_logger = logging.getLogger()

stdout_handler = logging.StreamHandler(stream=sys.stdout)
old_logger.addHandler(stdout_handler)

loguru.logger.remove(handler_id=0)
for handler in old_logger.handlers:
    loguru.logger.add(handler, format="<level>{message}</level>", colorize=True)

loguru.logger.info("info example")
loguru.logger.warning("warning example")
loguru==0.4.1
  - colorama [required: >=0.3.4, installed: 0.4.3]
  - win32-setctime [required: >=1.0.0, installed: 1.0.1]

and Windows 10

Thanks

@Delgan
Copy link
Owner

Delgan commented Apr 14, 2020

Hey @bataliero!

So, it might seem a bit technical, but here is the explanation. On Windows, sys.stdout needs to be wrapped using colorama to convert ansi code to actual colors. If you configure the logger using logger.add(sys.stdout), Loguru will detect that you are passing a "stream object" (that is, an object with a .write() method) and it will test the stream to know whether or not it should be internally wrapped. However, if you pass an instance of the standard logging.Handler, Loguru will not perform any verification nor wrapping as it does not have access to the stream, it will just write logs using handler.emit(). In such case, it is handler's responsibility to convert possible ansi codes. As you are using logging.StreamHandler(stream=sys.stdout), sys.stdout is never wrapped by colorama, and hence displays the raw ansi codes instead of the colors. This is working fine with git-bash because it is based on a terminal which natively implements ansi code coloration (like a Linux terminal).

A possible workaround is to call colorama.init() at the beginning of your script, this will automatically wrap sys.stdout so that it supports ansi codes (the same thing Loguru is doing internally). 😉

@Delgan Delgan added the question Further information is requested label Apr 18, 2020
@Delgan
Copy link
Owner

Delgan commented Jun 7, 2020

@bataliero Did the workaround provided solve you problem?

@bataliero
Copy link
Author

@Delgan, yes, it did, thanks for the answer!

@Delgan Delgan closed this as completed Jun 7, 2020
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

2 participants