summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichal Minar <miminar@redhat.com>2014-02-11 15:17:55 +0100
committerMichal Minar <miminar@redhat.com>2014-02-13 14:41:32 +0100
commit843acd4150a992e6de445d5868e0e7652b28509b (patch)
tree82b9482ea85983715572e93d8cdb6eacc5722f27
parentcfa8c207595781856fa4259b6102500cb7dd9eb5 (diff)
downloadopenlmi-scripts-843acd4150a992e6de445d5868e0e7652b28509b.tar.gz
openlmi-scripts-843acd4150a992e6de445d5868e0e7652b28509b.tar.xz
openlmi-scripts-843acd4150a992e6de445d5868e0e7652b28509b.zip
colorize error log messages
If running in terminal, make the levelnames colored. This makes the output much easier to read.
-rw-r--r--config/lmi.conf2
-rw-r--r--lmi/scripts/_metacommand/util.py4
-rw-r--r--lmi/scripts/common/lmi_logging.py73
3 files changed, 71 insertions, 8 deletions
diff --git a/config/lmi.conf b/config/lmi.conf
index 50d0160..349976b 100644
--- a/config/lmi.conf
+++ b/config/lmi.conf
@@ -46,7 +46,7 @@
# Format string used, when logging to a console. For details visit:
# http://docs.python.org/2/library/logging.html#logrecord-attributes
# This applies to levels more severe than INFO.
-#ConsoleFormat = %(levelname)-8s: %(message)s
+#ConsoleFormat = %(cseq)s%(levelname)-8s:%(creset)s %(message)s
# Format string used, when logging to a console. This applies to all messages
# with severity level up to INFO.
diff --git a/lmi/scripts/_metacommand/util.py b/lmi/scripts/_metacommand/util.py
index aabf588..96317df 100644
--- a/lmi/scripts/_metacommand/util.py
+++ b/lmi/scripts/_metacommand/util.py
@@ -34,6 +34,7 @@ Meta-command utility module.
import logging
import logging.config
import pkg_resources
+import platform
import re
import sys
import urlparse
@@ -134,7 +135,8 @@ def setup_logging(app_config, stderr=sys.stderr):
del cfg['handlers']['console']
cfg['root']['handlers'].remove('console')
- lmi_logging.setup_logger()
+ use_colors = platform.system() != 'Windows' and stderr.isatty()
+ lmi_logging.setup_logger(use_colors = use_colors)
logging.config.dictConfig(cfg)
def get_version(egg_name=PYTHON_EGG_NAME):
diff --git a/lmi/scripts/common/lmi_logging.py b/lmi/scripts/common/lmi_logging.py
index fb9c4f0..5402363 100644
--- a/lmi/scripts/common/lmi_logging.py
+++ b/lmi/scripts/common/lmi_logging.py
@@ -29,24 +29,79 @@
#
import logging
+TERM_COLOR_NAMES = (
+ 'black',
+ 'red',
+ 'green',
+ 'yellow',
+ 'blue',
+ 'magenta',
+ 'cyan',
+ 'white',
+ )
+
+# Following are terminal color codes of normal mode.
+CN_BLACK = 0
+CN_RED = 1
+CN_GREEN = 2
+CN_YELLOW = 3
+CN_BLUE = 4
+CN_MAGENTA = 5
+CN_CYAN = 6
+CN_WHITE = 7
+# Following are terminal color codes of bright mode.
+CB_BLACK = 8
+CB_RED = 9
+CB_GREEN = 10
+CB_YELLOW = 11
+CB_BLUE = 12
+CB_MAGENTA = 13
+CB_CYAN = 14
+CB_WHITE = 15
+
#: Default format string to use in stderr handlers.
-DEFAULT_FORMAT_STRING = "%(levelname_)-8s: %(message)s"
+DEFAULT_FORMAT_STRING = "%(cseq)s%(levelname_)-8s:%(creset)s %(message)s"
+
+WARNING_COLOR = CB_YELLOW
+ERROR_COLOR = CB_RED
+CRITICAL_COLOR = CB_MAGENTA
+
+LOG_LEVEL_2_COLOR = {
+ logging.WARNING : WARNING_COLOR,
+ logging.ERROR : ERROR_COLOR,
+ logging.CRITICAL: CRITICAL_COLOR
+}
class LogRecord(logging.LogRecord):
- def __init__(self, name, *args, **kwargs):
- logging.LogRecord.__init__(self, name, *args, **kwargs)
- self.levelname_ = self.levelname
+ def __init__(self, name, level, *args, **kwargs):
+ use_colors = kwargs.pop('use_colors', True)
+ logging.LogRecord.__init__(self, name, level, *args, **kwargs)
+ self.levelname_ = self.levelname.lower()
+ if use_colors and level >= logging.WARNING:
+ if level >= logging.CRITICAL:
+ color = LOG_LEVEL_2_COLOR[logging.CRITICAL]
+ elif level >= logging.ERROR:
+ color = LOG_LEVEL_2_COLOR[logging.ERROR]
+ else:
+ color = LOG_LEVEL_2_COLOR[logging.WARNING]
+ self.cseq = get_color_sequence(color)
+ self.creset = "\x1b[39m"
+ else:
+ self.cseq = self.creset = ''
class ScriptsLogger(logging.getLoggerClass()):
+ USE_COLORS = True
+
def makeRecord(self, name, level, fn, lno, msg, args, exc_info, func=None,
extra=None):
"""
Overriden method that just changes the *LogRecord* class to our
predefined.
"""
- rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
+ rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func,
+ use_colors=self.USE_COLORS)
if extra is not None:
for key in extra:
if (key in ["message", "asctime"]) or (key in rv.__dict__):
@@ -132,5 +187,11 @@ def get_logger(module_name):
return logging.getLogger(module_name)
return _logger
-def setup_logger():
+def get_color_sequence(color_code):
+ if color_code <= 7:
+ return "\x1b[%dm" % (30 + color_code)
+ return "\x1b[38;5;%dm" % color_code
+
+def setup_logger(use_colors=True):
+ ScriptsLogger.USE_COLORS = use_colors
logging.setLoggerClass(ScriptsLogger)