Giter Site home page Giter Site logo

Comments (4)

thorstenkampe avatar thorstenkampe commented on July 17, 2024

Further analysis: it seems like the above behaviour is the default behaviour. There is a class TTYColoredFormatter which should strip the escape sequences when the output device is not a TTY.

  • On Windows color escapes are stripped regardless of whether I use ColoredFormatter or TTYColoredFormatter. This is likely because Colorlog uses colorama on Windows with the default settings ("The default behaviour is to strip if on Windows or if output is redirected (not a tty)").

  • On Linux the color escapes are only stripped when redirecting stdout and using TTYColoredFormatter. This is of course a bug because logging can go to stdout and stderr but by default goes to stderr: "class logging.StreamHandler(stream=None) - [...] If stream is specified, the instance will use it for logging output; otherwise, sys.stderr will be used."

from python-colorlog.

thorstenkampe avatar thorstenkampe commented on July 17, 2024

Minimal example to demonstrate the issue - save this as logtest.py:

import colorlog
logger  = colorlog.getLogger()
handler = colorlog.StreamHandler()
handler.setFormatter(
    colorlog.TTYColoredFormatter('%(log_color)s%(message)s%(reset)s'))
logger.addHandler(handler)
logger.error('test')
#1
./logtest.py
test             # printed red (GOOD)

#2
./logtest.py > test.txt
test             # color stripped (BAD)

#3
./logtest.py 2> test.txt
cat -v test.txt
^[[31mtest^[[0m  # color escapes NOT stripped (BAD)

from python-colorlog.

thorstenkampe avatar thorstenkampe commented on July 17, 2024

On further investigation: when installing colorama on Linux - where it is not needed - issue #3 from above (redirecting stderr) goes away. Issue #2 (redirecting stdout) persists.

from python-colorlog.

borntyping avatar borntyping commented on July 17, 2024

This line should probably be changed so that the default stream a TTY formatter checks is stdout, matching the default stream used by the StreamHander: https://github.com/borntyping/python-colorlog/blob/master/colorlog/colorlog.py#L200

from python-colorlog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.