diff options
author | Ben Nemec <bnemec@redhat.com> | 2018-06-07 21:26:15 +0000 |
---|---|---|
committer | Ben Nemec <bnemec@redhat.com> | 2018-06-07 21:26:15 +0000 |
commit | 9d5065ea327f39198d0bfffcf865fbcc1afc7d5a (patch) | |
tree | 66fbceb9eb082f7377c32a992e7c49d729220174 | |
parent | 9da1f158b233b7212f010d726cca4f6230bd2e5f (diff) | |
download | oslo-log-9d5065ea327f39198d0bfffcf865fbcc1afc7d5a.tar.gz |
Automatically append reset_color to log lines
It's confusing that log colors can persist across multiple log lines
if they are not explicitly reset at the end of each line. This
change automatically appends the reset escape sequence to the end of
each log record passed through the ColorHandler so the behavior is
more what most people would expect.
Change-Id: I5fefdbdd7dd2ca437863bf2117b5a93b527e9776
-rw-r--r-- | oslo_log/handlers.py | 8 | ||||
-rw-r--r-- | oslo_log/tests/unit/test_log.py | 3 |
2 files changed, 5 insertions, 6 deletions
diff --git a/oslo_log/handlers.py b/oslo_log/handlers.py index d1240de..e8bc594 100644 --- a/oslo_log/handlers.py +++ b/oslo_log/handlers.py @@ -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 diff --git a/oslo_log/tests/unit/test_log.py b/oslo_log/tests/unit/test_log.py index 39b1cfc..7878525 100644 --- a/oslo_log/tests/unit/test_log.py +++ b/oslo_log/tests/unit/test_log.py @@ -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) |