Changeset View
Changeset View
Standalone View
Standalone View
pykolab/logger.py
Show All 29 Lines | |||||
class StderrToLogger(object): | class StderrToLogger(object): | ||||
""" | """ | ||||
Fake file-like stream object that redirects writes to a logger instance. | Fake file-like stream object that redirects writes to a logger instance. | ||||
""" | """ | ||||
def __init__(self, logger, log_level=logging.DEBUG): | def __init__(self, logger, log_level=logging.DEBUG): | ||||
self.logger = logger | self.logger = logger | ||||
self.log_level = log_level | self.log_level = log_level | ||||
self.linebuf = '' | self.linebuf = '' | ||||
self.skip_next = False | |||||
def write(self, buf): | def write(self, buf): | ||||
# ugly patch to make smtplib debug logging records appear on one line in log file | # ugly patch to make smtplib and smtpd debug logging records appear on one line in log file | ||||
# smtplib uses "print>>stderr, var, var" statements for debug logging. These | # smtplib uses "print>>stderr, var, var" statements for debug logging. These | ||||
# statements are splited into separate lines on separating whitespace. | # statements are splited into separate lines on separating whitespace. | ||||
for line in buf.rstrip().splitlines(): | for line in buf.rstrip().splitlines(): | ||||
if self.skip_next: | |||||
self.skip_next = False | |||||
continue | |||||
if buf != '\n': | if buf != '\n': | ||||
if line.startswith('send:') or line.startswith('reply:'): | linestarts = line.split(':')[0] | ||||
if linestarts in ['send', 'reply', 'Data', 'recips', 'Peer', 'sender']: | |||||
self.linebuf = line | self.linebuf = line | ||||
return | elif linestarts.startswith('===>'): | ||||
# Do not log lines starting with ====> | |||||
self.linebuf = '' | |||||
self.skip_next = True | |||||
continue | |||||
else: | else: | ||||
self.logger.log(self.log_level, '%s %s', self.linebuf, line.rstrip()[:150]) | self.logger.log(self.log_level, '%s %s', self.linebuf, line.rstrip()[:150]) | ||||
self.linebuf = '' | self.linebuf = '' | ||||
def flush(self): | def flush(self): | ||||
pass | pass | ||||
class LoggerAdapter(logging.LoggerAdapter): | |||||
""" | |||||
Custom LoggingAdapter to log Wallace mail message Queue ID | |||||
""" | |||||
def process(self, msg, kwargs): | |||||
return '%s %s' % (self.extra['qid'], msg), kwargs | |||||
class Logger(logging.Logger): | class Logger(logging.Logger): | ||||
""" | """ | ||||
The PyKolab version of a logger. | The PyKolab version of a logger. | ||||
This class wraps the Python native logging library, adding to the | This class wraps the Python native logging library, adding to the | ||||
loglevel capabilities, a debuglevel capability. | loglevel capabilities, a debuglevel capability. | ||||
""" | """ | ||||
debuglevel = 0 | debuglevel = 0 | ||||
▲ Show 20 Lines • Show All 181 Lines • Show Last 20 Lines |