From: Alfredo Deza Date: Wed, 4 Mar 2015 15:49:02 +0000 (-0500) Subject: move the log module into util X-Git-Tag: v1.2.2~9^2~8 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=82b870d3e6a8426a031922d6845c04d92f03efc0;p=radosgw-agent.git move the log module into util Signed-off-by: Alfredo Deza --- diff --git a/radosgw_agent/log.py b/radosgw_agent/log.py deleted file mode 100644 index f3ffeb3..0000000 --- a/radosgw_agent/log.py +++ /dev/null @@ -1,69 +0,0 @@ -import logging -import sys - -BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) - -COLORS = { - 'WARNING': YELLOW, - 'INFO': WHITE, - 'DEBUG': BLUE, - 'CRITICAL': RED, - 'ERROR': RED, - 'FATAL': RED, -} - -RESET_SEQ = "\033[0m" -COLOR_SEQ = "\033[1;%dm" -BOLD_SEQ = "\033[1m" - -BASE_COLOR_FORMAT = "%(asctime)s %(process)d\ - [$BOLD%(name)s$RESET][%(color_levelname)-17s] %(message)s" - -BASE_FORMAT = "%(asctime)s %(process)d [%(name)s][%(levelname)-6s] %(message)s" - - -def supports_color(): - """ - Returns True if the running system's terminal supports color, and False - otherwise. - """ - unsupported_platform = (sys.platform in ('win32', 'Pocket PC')) - # isatty is not always implemented - is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() - if unsupported_platform or not is_a_tty: - return False - return True - - -def color_message(message): - message = message.replace("$RESET", RESET_SEQ).replace("$BOLD", BOLD_SEQ) - return message - - -class ColoredFormatter(logging.Formatter): - """ - A very basic logging formatter that not only applies color to the levels of - the ouput but will also truncate the level names so that they do not alter - the visuals of logging when presented on the terminal. - """ - - def __init__(self, msg): - logging.Formatter.__init__(self, msg) - - def format(self, record): - levelname = record.levelname - truncated_level = record.levelname[:6] - levelname_color = COLOR_SEQ % (30 + COLORS[levelname]) + truncated_level + RESET_SEQ - record.color_levelname = levelname_color - return logging.Formatter.format(self, record) - - -def color_format(): - """ - Main entry point to get a colored formatter, it will use the - BASE_FORMAT by default and fall back to no colors if the system - does not support it - """ - str_format = BASE_COLOR_FORMAT if supports_color() else BASE_FORMAT - color_format = color_message(str_format) - return ColoredFormatter(color_format) diff --git a/radosgw_agent/util/log.py b/radosgw_agent/util/log.py new file mode 100644 index 0000000..f3ffeb3 --- /dev/null +++ b/radosgw_agent/util/log.py @@ -0,0 +1,69 @@ +import logging +import sys + +BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) + +COLORS = { + 'WARNING': YELLOW, + 'INFO': WHITE, + 'DEBUG': BLUE, + 'CRITICAL': RED, + 'ERROR': RED, + 'FATAL': RED, +} + +RESET_SEQ = "\033[0m" +COLOR_SEQ = "\033[1;%dm" +BOLD_SEQ = "\033[1m" + +BASE_COLOR_FORMAT = "%(asctime)s %(process)d\ + [$BOLD%(name)s$RESET][%(color_levelname)-17s] %(message)s" + +BASE_FORMAT = "%(asctime)s %(process)d [%(name)s][%(levelname)-6s] %(message)s" + + +def supports_color(): + """ + Returns True if the running system's terminal supports color, and False + otherwise. + """ + unsupported_platform = (sys.platform in ('win32', 'Pocket PC')) + # isatty is not always implemented + is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() + if unsupported_platform or not is_a_tty: + return False + return True + + +def color_message(message): + message = message.replace("$RESET", RESET_SEQ).replace("$BOLD", BOLD_SEQ) + return message + + +class ColoredFormatter(logging.Formatter): + """ + A very basic logging formatter that not only applies color to the levels of + the ouput but will also truncate the level names so that they do not alter + the visuals of logging when presented on the terminal. + """ + + def __init__(self, msg): + logging.Formatter.__init__(self, msg) + + def format(self, record): + levelname = record.levelname + truncated_level = record.levelname[:6] + levelname_color = COLOR_SEQ % (30 + COLORS[levelname]) + truncated_level + RESET_SEQ + record.color_levelname = levelname_color + return logging.Formatter.format(self, record) + + +def color_format(): + """ + Main entry point to get a colored formatter, it will use the + BASE_FORMAT by default and fall back to no colors if the system + does not support it + """ + str_format = BASE_COLOR_FORMAT if supports_color() else BASE_FORMAT + color_format = color_message(str_format) + return ColoredFormatter(color_format)