From 9da1f158b233b7212f010d726cca4f6230bd2e5f Mon Sep 17 00:00:00 2001 From: Ben Nemec Date: Fri, 1 Jun 2018 17:51:53 +0000 Subject: [PATCH] Provide reset_color key on log record When logging with color, it is necessary to reset the color at the end of each line to avoid the color bleeding into subsequent log lines. This change adds a new key to the record called 'reset_color' that serves as a shortcut to the escape sequence to reset the color. It also provides documentation for the ColorHandler class that explains how to use it. Change-Id: I68f1c716cbed241f79fa65dae1affe810b8e6e30 Closes-Bug: 1731477 --- oslo_log/handlers.py | 7 +++++++ oslo_log/tests/unit/test_log.py | 7 ++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/oslo_log/handlers.py b/oslo_log/handlers.py index 74816189..d1240dea 100644 --- a/oslo_log/handlers.py +++ b/oslo_log/handlers.py @@ -146,6 +146,12 @@ 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. + """ LEVEL_COLORS = { _TRACE: '\033[00;35m', # MAGENTA logging.DEBUG: '\033[00;32m', # GREEN @@ -158,4 +164,5 @@ 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) diff --git a/oslo_log/tests/unit/test_log.py b/oslo_log/tests/unit/test_log.py index 669081c8..39b1cfc5 100644 --- a/oslo_log/tests/unit/test_log.py +++ b/oslo_log/tests/unit/test_log.py @@ -987,7 +987,8 @@ class FancyRecordTestCase(LogTestBase): "[%(request_id)s]: " "%(instance)s" "%(resource)s" - "%(message)s", + "%(message)s" + "%(reset_color)s", logging_default_format_string="%(missing)s: %(message)s") self.colorlog = log.getLogger() self._add_handler_with_cleanup(self.colorlog, handlers.ColorHandler) @@ -1040,7 +1041,7 @@ class FancyRecordTestCase(LogTestBase): fake_resource = {'name': resource} message = 'info' self.colorlog.info(message, context=ctxt, resource=fake_resource) - expected = ('%s [%s]: [%s] %s\n' % + expected = ('%s [%s]: [%s] %s\033[00m\n' % (color, ctxt.request_id, resource, message)) self.assertEqual(expected, self.stream.getvalue()) @@ -1053,7 +1054,7 @@ class FancyRecordTestCase(LogTestBase): 'id': resource_id} message = 'info' self.colorlog.info(message, context=ctxt, resource=fake_resource) - expected = ('%s [%s]: [%s-%s] %s\n' % + expected = ('%s [%s]: [%s-%s] %s\033[00m\n' % (color, ctxt.request_id, type, resource_id, message)) self.assertEqual(expected, self.stream.getvalue())