From df80f437ac69cb923f06a1c42d3c55903de98eac Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 1 Jun 2012 23:12:56 +0000 Subject: [PATCH] Add user_name project_name and color option to log * adds project_name and user_name to context for logging * adds color argument to logging which allows for colored log output based on the log level Change-Id: If37d646fdba77d4214f72b19e5df02da5f7dbac6 --- nova/log.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/nova/log.py b/nova/log.py index 310656bc5..19ad95b70 100644 --- a/nova/log.py +++ b/nova/log.py @@ -279,6 +279,21 @@ class LegacyNovaFormatter(logging.Formatter): return '\n'.join(formatted_lines) +class NovaColorHandler(logging.StreamHandler): + LEVEL_COLORS = { + logging.DEBUG: '\033[00;32m', # GREEN + logging.INFO: '\033[00;36m', # CYAN + logging.AUDIT: '\033[01;36m', # BOLD CYAN + logging.WARN: '\033[01;33m', # BOLD YELLOW + logging.ERROR: '\033[01;31m', # BOLD RED + logging.CRITICAL: '\033[01;31m', # BOLD RED + } + + def format(self, record): + record.color = self.LEVEL_COLORS[record.levelno] + return logging.StreamHandler.format(self, record) + + class PublishErrorsHandler(logging.Handler): def emit(self, record): if 'list_notifier_drivers' in FLAGS: @@ -357,7 +372,7 @@ def _setup_logging_from_flags(): os.chmod(logpath, mode) if FLAGS.use_stderr: - streamlog = logging.StreamHandler() + streamlog = NovaColorHandler() nova_root.addHandler(streamlog) elif not FLAGS.log_file: