Closed
Description
Colors are not working in default loguru
configuration for mintty
terminal (windows)
- loguru starts with a default sink
sys.stderr
- loguru finds that
sys.stderr
has a callablewrite
attribute and callsAnsiToWin32(...)
- It's windows.
AnsiToWin32
works without errors.
As result ANSI color codes are replaced with WinAPI calls and mintty
is not able to handle it.
I was able to workaround this behavior by the code snippet below. Think it might be useful for somebody else. The trick is to configure loguru
to use 'callable' sink instead of stream-like object.
import sys
import colorama
from loguru import logger
def setup_ansi_colors(suppress_colors):
convert_ansi_codes_to_win32_calls = False
if os.name == 'nt':
# Only need to init colorama with 'convert=True' when app is called
# from 'cmd.exe', 'powershell' or 'git-bash via VS Code'
convert_ansi_codes_to_win32_calls = 'TERM' not in os.environ or \
os.environ.get('TERM_PROGRAM', None) == 'vscode'
if 'CONVERT_ANSI_CODES_TO_WIN32_CALLS' in os.environ:
# explicit option is useful for cases when automatic guess fails (e.g. for Eclipse IDE)
convert_ansi_codes_to_win32_calls = os.environ.get('CONVERT_ANSI_CODES_TO_WIN32_CALLS').lower() in ('true', '1')
colorama.init(strip=suppress_colors, convert=convert_ansi_codes_to_win32_calls)
setup_ansi_colors(suppress_colors=False)
logger.remove()
logger.add(sink=sys.stdout.write, colorize=True)