freelan-developers / chromalog

Enhance Python with colored logging
MIT License
18 stars 3 forks source link

turning on color support for custom terminals #5

Open ayinger1 opened 7 years ago

ayinger1 commented 7 years ago

in the docs, it says if chromalog detects non-color supporting terminal, it will fall back on monochrome. but i have a custom terminal (written in java) where i would like to launch a python process (using chromalog) and have it continue to output ansi color codes, even though chromalog thinks the python process is running in non-ansi environment.

so, is there a way to turn off this auto-detecting ansi terminal behavior, so that it always logs with ansi codes?

(looking thru the code i see that in stream.py, function stream_has_color_support checks for color support: getattr(stream, 'isatty', lambda: False)()

is there a simple way to initialize logging with a stream where the 'isatty' is set to True?)

ayinger1 commented 7 years ago

here is my current solution to this issue:

# proxy class to fool chromalog into thinking we are *always* isatty:
class PseudoTTY(object):
    def __init__(self, underlying):
        self.__underlying = underlying
    def __getattr__(self, name):
        return getattr(self.__underlying, name)
    def isatty(self):
        return True
# wrap PseudoTTY proxy around sys.stderr and use like so:
   chromalog.basicConfig(format=logformat, level=logging.INFO, stream=PseudoTTY(sys.stderr))