tartley / colorama

Simple cross-platform colored terminal text in Python
BSD 3-Clause "New" or "Revised" License
3.56k stars 252 forks source link

ansi char stripping does not work in logging #372

Open fopina opened 1 year ago

fopina commented 1 year ago

Using init(strip=True) to disable colors works for print statements, such as:

>>> from colorama import init, Fore
>>> print(f'{Fore.RED}test{Fore.RESET}')
test (COLORED)
>>> init(strip=True)
>>> print(f'{Fore.RED}test{Fore.RESET}')
test (NO COLOR)

But it does not for logging statements

>>> from colorama import init, Fore
>>> init(strip=True)
>>> import logging
>>> logging.error(f'{Fore.RED}oi{Fore.RESET}')
ERROR:root:oi (STILL COLORED)

I'm currently using this workaround in my CLI main/parser entrypoint:

        # FIXME: colorama strip fails in logging...!
        # monkeypatch Fore
        class FakeFore:
            def __getattribute__(self, __name: str):
                return ''

        Fore.__class__ = FakeFore

But it would be nice to have stripping actually working...