Add log source to log messages

This commit is contained in:
Anders Ingemann 2014-11-25 21:37:35 +01:00
parent 0f4c08e51d
commit 31c453070f

View file

@ -17,7 +17,7 @@ def get_console_handler(debug, colorize):
console_handler = logging.StreamHandler(sys.stderr) console_handler = logging.StreamHandler(sys.stderr)
if colorize: if colorize:
# We want to colorize the output to the console, so we add a formatter # We want to colorize the output to the console, so we add a formatter
console_handler.setFormatter(ConsoleFormatter()) console_handler.setFormatter(ColorFormatter())
# Set the log level depending on the debug argument # Set the log level depending on the debug argument
if debug: if debug:
console_handler.setLevel(logging.DEBUG) console_handler.setLevel(logging.DEBUG)
@ -66,8 +66,23 @@ def get_log_filename(manifest_path):
return filename return filename
class ConsoleFormatter(logging.Formatter): class SourceFormatter(logging.Formatter):
"""Formats log statements for the console """Adds a [source] tag to the log message if it exists
The python docs suggest using a LoggingAdapter, but that would mean we'd
have to use it everywhere we log something (and only when called remotely),
which is not feasible.
"""
def format(self, record):
extra = getattr(record, 'extra', {})
if 'source' in extra:
record.msg = '[{source}] {message}'.format(source=record.extra['source'],
message=record.msg)
return super(SourceFormatter, self).format(record)
class ColorFormatter(SourceFormatter):
"""Colorizes log messages depending on the loglevel
""" """
level_colors = {logging.ERROR: 'red', level_colors = {logging.ERROR: 'red',
logging.WARNING: 'magenta', logging.WARNING: 'magenta',
@ -75,14 +90,13 @@ class ConsoleFormatter(logging.Formatter):
} }
def format(self, record): def format(self, record):
if(record.levelno in self.level_colors): # Colorize the message if we have a color for it (DEBUG has no color)
# Colorize the message if we have a color for it (DEBUG has no color) from termcolor import colored
from termcolor import colored record.msg = colored(record.msg, self.level_colors.get(record.levelno, None))
record.msg = colored(record.msg, self.level_colors[record.levelno]) return super(ColorFormatter, self).format(record)
return super(ConsoleFormatter, self).format(record)
class FileFormatter(logging.Formatter): class FileFormatter(SourceFormatter):
"""Formats log statements for output to file """Formats log statements for output to file
Currently this is just a stub Currently this is just a stub
""" """
@ -116,4 +130,6 @@ class LogServer(object):
import pickle import pickle
record = pickle.loads(pickled_record) record = pickle.loads(pickled_record)
log = logging.getLogger() log = logging.getLogger()
record.extra = getattr(record, 'extra', {})
record.extra['source'] = 'remote'
log.handle(record) log.handle(record)