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']: | |||||
machniak: Will that not match e.g. "send something" line? I mean w/o the colon. | |||||
adomaitisAuthorUnsubmitted Done Inline ActionsThird party python libs logs everything to stderr. To catch that we need a file type object which should redirect everything to pykolab logger. For some reason some libraries produce duplicated log messages. That makes reading logs confusing. The idea here is to filter out duplicated messages. So yes, "send something" should be caught, but as this is applied to log records going to stderr it should not filter anything produced by pykolab.logger . adomaitis: Third party python libs logs everything to stderr. To catch that we need a file type object… | |||||
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 Logger(logging.Logger): | class Logger(logging.Logger): | ||||
▲ Show 20 Lines • Show All 188 Lines • Show Last 20 Lines |
Will that not match e.g. "send something" line? I mean w/o the colon.