Incorporated Kuba's feedback

This commit is contained in:
Brad Warren
2015-09-26 12:18:32 -07:00
parent 817aadae6a
commit 2015811a6c
4 changed files with 17 additions and 19 deletions
+4 -11
View File
@@ -15,13 +15,12 @@ class StreamHandler(logging.StreamHandler):
:ivar bool red_level: The level at which to output :ivar bool red_level: The level at which to output
""" """
_RED = '\033[31m'
def __init__(self, stream=None): def __init__(self, stream=None):
super(StreamHandler, self).__init__(stream) super(StreamHandler, self).__init__(stream)
self.colored = (sys.stderr.isatty() if stream is None else self.colored = (sys.stderr.isatty() if stream is None else
stream.isatty()) stream.isatty())
self.set_red_level(logging.WARNING) self.red_level = logging.WARNING
def format(self, record): def format(self, record):
"""Formats the string representation of record. """Formats the string representation of record.
@@ -34,14 +33,8 @@ class StreamHandler(logging.StreamHandler):
""" """
output = super(StreamHandler, self).format(record) output = super(StreamHandler, self).format(record)
if self.colored and record.levelno >= self.red_level: if self.colored and record.levelno >= self.red_level:
return ''.join((self._RED, output, le_util.ANSI_SGR_RESET)) return ''.join((le_util.ANSI_SGR_RED,
output,
le_util.ANSI_SGR_RESET))
else: else:
return output return output
def set_red_level(self, red_level):
"""Sets the level necessary to display output in red.
:param int red_level: Minimum log level for displaying red text
"""
self.red_level = red_level
+6 -1
View File
@@ -18,7 +18,12 @@ Key = collections.namedtuple("Key", "file pem")
CSR = collections.namedtuple("CSR", "file data form") CSR = collections.namedtuple("CSR", "file data form")
# ANSI escape code for resetting output format # ANSI SGR escape codes
# Formats text as bold or with increased intensity
ANSI_SGR_BOLD = '\033[1m'
# Colors text red
ANSI_SGR_RED = "\033[31m"
# Resets output format
ANSI_SGR_RESET = "\033[0m" ANSI_SGR_RESET = "\033[0m"
+1 -2
View File
@@ -31,7 +31,6 @@ class Reporter(object):
LOW_PRIORITY = 2 LOW_PRIORITY = 2
"""Low priority constant. See `add_message`.""" """Low priority constant. See `add_message`."""
_BOLD = '\033[1m'
_msg_type = collections.namedtuple('ReporterMsg', 'priority text on_crash') _msg_type = collections.namedtuple('ReporterMsg', 'priority text on_crash')
def __init__(self): def __init__(self):
@@ -76,7 +75,7 @@ class Reporter(object):
no_exception = sys.exc_info()[0] is None no_exception = sys.exc_info()[0] is None
bold_on = sys.stdout.isatty() bold_on = sys.stdout.isatty()
if bold_on: if bold_on:
print self._BOLD print le_util.ANSI_SGR_BOLD
print 'IMPORTANT NOTES:' print 'IMPORTANT NOTES:'
first_wrapper = textwrap.TextWrapper( first_wrapper = textwrap.TextWrapper(
initial_indent=' - ', subsequent_indent=(' ' * 3)) initial_indent=' - ', subsequent_indent=(' ' * 3))
+6 -5
View File
@@ -7,6 +7,7 @@ from letsencrypt import le_util
class StreamHandlerTest(unittest.TestCase): class StreamHandlerTest(unittest.TestCase):
"""Tests for letsencrypt.colored_logging."""
def setUp(self): def setUp(self):
from letsencrypt import colored_logging from letsencrypt import colored_logging
@@ -26,13 +27,13 @@ class StreamHandlerTest(unittest.TestCase):
def test_format_and_red_level(self): def test_format_and_red_level(self):
msg = 'I did another thing' msg = 'I did another thing'
self.handler.set_red_level(logging.DEBUG) self.handler.red_level = logging.DEBUG
self.logger.debug(msg) self.logger.debug(msg)
# pylint: disable=protected-access self.assertEqual(self.stream.getvalue(),
expected = '{0}{1}{2}\n'.format(self.handler._RED, msg, '{0}{1}{2}\n'.format(le_util.ANSI_SGR_RED,
le_util.ANSI_SGR_RESET) msg,
self.assertEqual(self.stream.getvalue(), expected) le_util.ANSI_SGR_RESET))
if __name__ == "__main__": if __name__ == "__main__":