You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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). 😉
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:
Thanks
The text was updated successfully, but these errors were encountered: