Comments (4)
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
orTTYColoredFormatter
. 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.
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.
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.
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)
- No module named 'colorlog' HOT 1
- v4.8.0 - ColoredRecord does not set asctime property. HOT 2
- 5.0.0: test suite warnings HOT 2
- 'bold' option is not working HOT 1
- Typing information is missing on import. HOT 2
- ModuleNotFoundError: No module named 'colorlog' HOT 1
- Purple - PowerShell HOT 1
- hope add predefined level such as colorlog.ERROR colorlog.DEBUG and so on HOT 1
- [Bug] colorlog.basicConfig() doesn't seems to work when upgrading from v5 to v6 HOT 3
- colorlog introduces "[1m[0m"-like entries in logs running on argo workflows HOT 2
- Logging is sometimes duplicated without color HOT 5
- How to make `print()` statement output colorful?
- Doesn't support defaults HOT 1
- Document `light_*` colors? HOT 4
- Update default_log_colors from log_colors instead of replace it HOT 3
- Failed to add color to log HOT 4
- Docu like ReadTheDocs / Offering a objects.inv HOT 1
- Duplicative stream definition required due to design choice made in #64 HOT 1
- support *stacklevel* argument HOT 3
- colorlog.basicConfig does not accept '{' style HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-colorlog.