Merge "Automatically append reset_color to log lines"

This commit is contained in:
Zuul 2018-06-25 16:35:49 +00:00 committed by Gerrit Code Review
commit 3b43002a31
2 changed files with 5 additions and 6 deletions

View File

@ -148,9 +148,9 @@ class OSJournalHandler(logging.Handler):
class ColorHandler(logging.StreamHandler):
"""Log handler that sets the 'color' key based on the level
To use, include a '%(color)s' entry in the logging_context_format_string
and a '%(reset_color)s' entry at the end of the format string so that the
color setting does not persist between log lines.
To use, include a '%(color)s' entry in the logging_context_format_string.
There is also a '%(reset_color)s' key that can be used to manually reset
the color within a log line.
"""
LEVEL_COLORS = {
_TRACE: '\033[00;35m', # MAGENTA
@ -165,4 +165,4 @@ class ColorHandler(logging.StreamHandler):
def format(self, record):
record.color = self.LEVEL_COLORS[record.levelno]
record.reset_color = '\033[00m'
return logging.StreamHandler.format(self, record)
return logging.StreamHandler.format(self, record) + record.reset_color

View File

@ -987,8 +987,7 @@ class FancyRecordTestCase(LogTestBase):
"[%(request_id)s]: "
"%(instance)s"
"%(resource)s"
"%(message)s"
"%(reset_color)s",
"%(message)s",
logging_default_format_string="%(missing)s: %(message)s")
self.colorlog = log.getLogger()
self._add_handler_with_cleanup(self.colorlog, handlers.ColorHandler)