diff --git a/coloredlogs/__init__.py b/coloredlogs/__init__.py index d279180..ba5a572 100644 --- a/coloredlogs/__init__.py +++ b/coloredlogs/__init__.py @@ -1,7 +1,7 @@ # Colored terminal output for Python's logging module. # # Author: Peter Odding -# Last Change: February 16, 2020 +# Last Change: December 8, 2020 # URL: https://coloredlogs.readthedocs.io """ @@ -384,7 +384,7 @@ def install(level=None, **kw): """ logger = kw.get('logger') or logging.getLogger() reconfigure = kw.get('reconfigure', True) - stream = kw.get('stream', None) + stream = kw.get('stream', sys.stderr) style = check_style(kw.get('style') or DEFAULT_FORMAT_STYLE) # Get the log level from an argument, environment variable or default and # convert the names of log levels to numbers to enable numeric comparison. diff --git a/coloredlogs/tests.py b/coloredlogs/tests.py index 6ccea8a..1c64499 100644 --- a/coloredlogs/tests.py +++ b/coloredlogs/tests.py @@ -1,7 +1,7 @@ # Automated tests for the `coloredlogs' package. # # Author: Peter Odding -# Last Change: February 16, 2020 +# Last Change: December 8, 2020 # URL: https://coloredlogs.readthedocs.io """Automated tests for the `coloredlogs` package.""" @@ -18,7 +18,7 @@ # External dependencies. from humanfriendly.compat import StringIO -from humanfriendly.terminal import ANSI_COLOR_CODES, ansi_style, ansi_wrap +from humanfriendly.terminal import ANSI_COLOR_CODES, ANSI_CSI, ansi_style, ansi_wrap from humanfriendly.testing import PatchedAttribute, PatchedItem, TestCase, retry from humanfriendly.text import format, random_string @@ -389,6 +389,35 @@ def test_plain_text_output_format(self): assert severity in last_line assert PLAIN_TEXT_PATTERN.match(last_line) + def test_auto_disable(self): + """ + Make sure ANSI escape sequences are not emitted when logging output is being redirected. + + This is a regression test for https://github.com/xolox/python-coloredlogs/issues/100. + + It works as follows: + + 1. We mock an interactive terminal using 'capturer' to ensure that this + test works inside test drivers that capture output (like pytest). + + 2. We launch a subprocess (to ensure a clean process state) where + stderr is captured but stdout is not, emulating issue #100. + + 3. The output captured on stderr contained ANSI escape sequences after + this test was written and before the issue was fixed, so now this + serves as a regression test for issue #100. + """ + with CaptureOutput(): + interpreter = subprocess.Popen([ + sys.executable, "-c", ";".join([ + "import coloredlogs, logging", + "coloredlogs.install()", + "logging.info('Hello world')", + ]), + ], stderr=subprocess.PIPE) + stdout, stderr = interpreter.communicate() + assert ANSI_CSI not in stderr.decode('UTF-8') + def test_html_conversion(self): """Check the conversion from ANSI escape sequences to HTML.""" # Check conversion of colored text.