Changeset View
Changeset View
Standalone View
Standalone View
pykolab/logger.py
Show All 21 Lines | |||||
import logging.handlers | import logging.handlers | ||||
import os | import os | ||||
import pwd | import pwd | ||||
import sys | import sys | ||||
import time | import time | ||||
from pykolab.translate import _ | from pykolab.translate import _ | ||||
class StderrToLogger(object): | |||||
""" | |||||
Fake file-like stream object that redirects writes to a logger instance. | |||||
""" | |||||
def __init__(self, logger, log_level=logging.DEBUG): | |||||
self.logger = logger | |||||
self.log_level = log_level | |||||
self.linebuf = '' | |||||
def write(self, buf): | |||||
# ugly patch to make smtplib debug logging records appear on one line in log file | |||||
# smtplib uses "print>>stderr, var, var" statements for debug logging. These | |||||
# statements are splited into separate lines on separating whitespace. | |||||
for line in buf.rstrip().splitlines(): | |||||
if buf != '\n': | |||||
if line.startswith('send:') or line.startswith('reply:'): | |||||
self.linebuf = line | |||||
return | |||||
else: | |||||
self.logger.log(self.log_level, '%s %s', self.linebuf, line.rstrip()[:150]) | |||||
self.linebuf = '' | |||||
def flush(self): | |||||
pass | |||||
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 55 Lines • ▼ Show 20 Lines | def __init__(self, *args, **kw): | ||||
name = kw['name'] | name = kw['name'] | ||||
elif len(args) == 1: | elif len(args) == 1: | ||||
name = args[0] | name = args[0] | ||||
else: | else: | ||||
name = 'pykolab' | name = 'pykolab' | ||||
logging.Logger.__init__(self, name) | logging.Logger.__init__(self, name) | ||||
plaintextformatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s") | plaintextformatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s [%(process)d] %(message)s") | ||||
if not self.fork: | if not self.fork: | ||||
self.console_stdout = logging.StreamHandler(sys.stdout) | self.console_stdout = logging.StreamHandler(sys.stdout) | ||||
self.console_stdout.setFormatter(plaintextformatter) | self.console_stdout.setFormatter(plaintextformatter) | ||||
self.addHandler(self.console_stdout) | self.addHandler(self.console_stdout) | ||||
if kw.has_key('logfile'): | if kw.has_key('logfile'): | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | class Logger(logging.Logger): | ||||
def debug(self, msg, level=1, *args, **kw): | def debug(self, msg, level=1, *args, **kw): | ||||
self.setLevel(self.loglevel) | self.setLevel(self.loglevel) | ||||
# Work around other applications not using various levels of debugging | # Work around other applications not using various levels of debugging | ||||
if not self.name.startswith('pykolab') and not self.debuglevel == 9: | if not self.name.startswith('pykolab') and not self.debuglevel == 9: | ||||
return | return | ||||
if level <= self.debuglevel: | if level <= self.debuglevel: | ||||
# TODO: Not the way it's supposed to work! | # TODO: Not the way it's supposed to work! | ||||
self.log(logging.DEBUG, '[%d]: %s' % (os.getpid(),msg)) | self.log(logging.DEBUG, msg) | ||||
logging.setLoggerClass(Logger) | logging.setLoggerClass(Logger) |