提交 ed4bf8bc 编写于 作者: L Lukáš Doktor

output: Allow the LoggingFile to use different prefixes per logger

We are about to need to allow different log prefixes per logger in our
LoggingFile helper class. Let's allow it by using list of prefixes,
instead of a single prefix.
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 66ee87b2
......@@ -607,22 +607,23 @@ class LoggingFile(object):
File-like object that will receive messages pass them to logging.
"""
def __init__(self, prefix='', level=logging.DEBUG,
def __init__(self, prefixes=None, level=logging.DEBUG,
loggers=None):
"""
Constructor. Sets prefixes and which loggers are going to be used.
:param prefix - The prefix for each line logged by this object.
:param prefixes: Prefix per logger to be prefixed to each line.
:param level: Log level to be used when writing messages.
:param loggers: Loggers into which write should be issued. (list)
"""
self._prefix = prefix
if not loggers:
loggers = [logging.getLogger()]
self._level = level
self._buffer = []
self._loggers = loggers
if prefixes is None:
prefixes = [""] * len(loggers)
self._prefixes = prefixes
def write(self, data):
""""
......@@ -653,8 +654,8 @@ class LoggingFile(object):
"""
Passes lines of output to the logging module.
"""
for lg in self._loggers:
lg.log(self._level, self._prefix + line)
for logger, prefix in zip(self._loggers, self._prefixes):
logger.log(self._level, prefix + line)
def _flush_buffer(self):
if self._buffer:
......@@ -667,11 +668,14 @@ class LoggingFile(object):
def isatty(self):
return False
def add_logger(self, logger):
def add_logger(self, logger, prefix=""):
self._loggers.append(logger)
self._prefixes.append(prefix)
def rm_logger(self, logger):
idx = self._loggers.index(logger)
self._loggers.remove(logger)
self._prefixes = self._prefixes[:idx] + self._prefixes[idx+1:]
class Throbber(object):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册