Skip to content

Commit

Permalink
Bug fix for issue #84 (force enable colored text)
Browse files Browse the repository at this point in the history
Details: #84
  • Loading branch information
xolox committed Dec 9, 2020
1 parent 4f8c9aa commit ddd5aa3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 4 additions & 3 deletions coloredlogs/__init__.py
@@ -1,7 +1,7 @@
# Colored terminal output for Python's logging module.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: December 8, 2020
# Last Change: December 10, 2020
# URL: https://coloredlogs.readthedocs.io

"""
Expand Down Expand Up @@ -429,8 +429,9 @@ def install(level=None, **kw):
# Try to enable Windows native ANSI support or Colorama.
if on_windows():
use_colors = enable_ansi_support()
# Disable ANSI escape sequences if 'stream' isn't connected to a terminal.
if use_colors or use_colors is None:
# Disable ANSI escape sequences if 'stream' isn't connected
# to a terminal and no override (isatty=True) is used.
if use_colors is None:
use_colors = terminal_supports_colors(stream)
# Create a stream handler.
handler = logging.StreamHandler(stream) if stream else StandardErrorHandler()
Expand Down
16 changes: 15 additions & 1 deletion coloredlogs/tests.py
@@ -1,7 +1,7 @@
# Automated tests for the `coloredlogs' package.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: December 8, 2020
# Last Change: December 10, 2020
# URL: https://coloredlogs.readthedocs.io

"""Automated tests for the `coloredlogs` package."""
Expand Down Expand Up @@ -389,6 +389,20 @@ def test_plain_text_output_format(self):
assert severity in last_line
assert PLAIN_TEXT_PATTERN.match(last_line)

def test_force_enable(self):
"""
Make sure ANSI escape sequences can be forced (bypassing auto-detection).
"""
interpreter = subprocess.Popen([
sys.executable, "-c", ";".join([
"import coloredlogs, logging",
"coloredlogs.install(isatty=True)",
"logging.info('Hello world')",
]),
], stderr=subprocess.PIPE)
stdout, stderr = interpreter.communicate()
assert ANSI_CSI in stderr.decode('UTF-8')

def test_auto_disable(self):
"""
Make sure ANSI escape sequences are not emitted when logging output is being redirected.
Expand Down

0 comments on commit ddd5aa3

Please sign in to comment.