Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the requested log level when handling broken stdout pipe #6153

Merged
merged 1 commit into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/pip/_internal/cli/base_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def main(self, args):
# Set verbosity so that it can be used elsewhere.
self.verbosity = options.verbose - options.quiet

setup_logging(
level_number = setup_logging(
verbosity=self.verbosity,
no_color=options.no_color,
user_log_file=options.log,
Expand Down Expand Up @@ -197,7 +197,7 @@ def main(self, args):
# Bypass our logger and write any remaining messages to stderr
# because stdout no longer works.
print('ERROR: Pipe to stdout was broken', file=sys.stderr)
if logger.getEffectiveLevel() <= logging.DEBUG:
if level_number <= logging.DEBUG:
traceback.print_exc(file=sys.stderr)

return ERROR
Expand Down
6 changes: 6 additions & 0 deletions src/pip/_internal/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ def filter(self, record):

def setup_logging(verbosity, no_color, user_log_file):
"""Configures and sets up all of the logging

Returns the requested logging level, as its integer value.
"""

# Determine the level to be logging at.
Expand All @@ -230,6 +232,8 @@ def setup_logging(verbosity, no_color, user_log_file):
else:
level = "INFO"

level_number = getattr(logging, level)

# The "root" logger should match the "console" level *unless* we also need
# to log to a user log file.
include_user_log = user_log_file is not None
Expand Down Expand Up @@ -310,3 +314,5 @@ def setup_logging(verbosity, no_color, user_log_file):
}
},
})

return level_number
18 changes: 18 additions & 0 deletions tests/functional/test_broken_stdout.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import subprocess
import sys

Expand Down Expand Up @@ -42,6 +43,23 @@ def test_broken_stdout_pipe(deprecated_python):
assert returncode == _BROKEN_STDOUT_RETURN_CODE


def test_broken_stdout_pipe__log_option(deprecated_python, tmpdir):
"""
Test a broken pipe to stdout when --log is passed.
"""
log_path = os.path.join(str(tmpdir), 'log.txt')
stderr, returncode = setup_broken_stdout_test(
['pip', '--log', log_path, 'list'],
deprecated_python=deprecated_python,
)

# Check that no traceback occurs.
assert 'raise BrokenStdoutLoggingError()' not in stderr
assert stderr.count('Traceback') == 0

assert returncode == _BROKEN_STDOUT_RETURN_CODE


def test_broken_stdout_pipe__verbose(deprecated_python):
"""
Test a broken pipe to stdout with verbose logging enabled.
Expand Down