Skip to content

Commit

Permalink
Use the requested log level when handling broken stdout pipe.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjerdonek committed Jan 21, 2019
1 parent fc9bb60 commit 9d00420
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
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

0 comments on commit 9d00420

Please sign in to comment.