Skip to content

Commit

Permalink
Breaking change: require a stream to be set in TTYColoredFormatter
Browse files Browse the repository at this point in the history
This avoids assuming where logs are being written, and ensures
everything works as expected when logs are redirected.

Closes #64.
  • Loading branch information
borntyping committed Dec 14, 2018
1 parent 31691fb commit 40280b4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion colorlog/colorlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class TTYColoredFormatter(ColoredFormatter):

def __init__(self, *args, **kwargs):
"""Overwrite the `reset` argument to False if stream is not a TTY."""
self.stream = kwargs.pop('stream', sys.stdout)
self.stream = kwargs.pop('stream')

# Both `reset` and `isatty` must be true to insert reset codes.
kwargs['reset'] = kwargs.get('reset', True) and self.stream.isatty()
Expand Down
15 changes: 12 additions & 3 deletions colorlog/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
import logging
import sys

import pytest

import colorlog

import pytest
import testing_stream_handler

class TestingStreamHandler(logging.StreamHandler):
"""Raise errors to be caught by py.test instead of printing to stdout."""

def handleError(self, record):
_type, value, _traceback = sys.exc_info()
raise value


def assert_log_message(log_function, message, capsys):
Expand Down Expand Up @@ -46,6 +53,7 @@ def function(logger, validator=None):
assert valid, "{!r} did not validate".format(line.strip())

return lines

return function


Expand All @@ -57,7 +65,7 @@ def function(*args, **kwargs):

formatter = formatter_cls(*args, **kwargs)

stream = testing_stream_handler.TestingStreamHandler()
stream = TestingStreamHandler(stream=sys.stderr)
stream.setLevel(logging.DEBUG)
stream.setFormatter(formatter)

Expand All @@ -66,4 +74,5 @@ def function(*args, **kwargs):
logger.addHandler(stream)

return test_logger(logger, validator)

return function
11 changes: 7 additions & 4 deletions colorlog/tests/test_colorlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ def test_template_style(create_and_test_logger):


def test_ttycolorlog(create_and_test_logger, monkeypatch):
monkeypatch.setattr(sys.stdout, 'isatty', lambda: True)
monkeypatch.setattr(sys.stderr, 'isatty', lambda: True)
create_and_test_logger(
formatter_class=colorlog.TTYColoredFormatter,
validator=lambda line: '\x1b[' in line)
validator=lambda line: '\x1b[' in line,
stream=sys.stderr)


def test_ttycolorlog_notty(create_and_test_logger, monkeypatch):
monkeypatch.setattr(sys.stdout, 'isatty', lambda: False)
monkeypatch.setattr(sys.stderr, 'isatty', lambda: False)
create_and_test_logger(
formatter_class=colorlog.TTYColoredFormatter,
validator=lambda line: '\x1b[' not in line)
validator=lambda line: '\x1b[' not in line,
stream=sys.stderr)
11 changes: 0 additions & 11 deletions colorlog/tests/testing_stream_handler.py

This file was deleted.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='colorlog',
version='3.2.0',
version='4.0.0',

description='Log formatting with colors!',
long_description=open('README.md').read(),
Expand Down

0 comments on commit 40280b4

Please sign in to comment.