logger

Module logger |
[hide private]
[frames] | no frames]

## Source Code for Module logger

     1
     2  from configParser import C3Object
     3  from PyZ3950.CQLParser import PrefixableObject
     4  import os, time, sys
     5  from baseObjects import Session, Logger
     6
     7 -class SimpleLogger(Logger):
     8
     9      fileh = None
    10      lineCache = []
    11      cacheLen = 0
    12
    13 -    def __init__(self, session, config, parent):
    14          Logger.__init__(self, session, config, parent)
    15          fp = self.get_path(None, 'filePath')
    16          if (fp == "stdout"):
    17              self.fileh = sys.stdout
    18          elif (fp == "stderr"):
    19              self.fileh = sys.stderr
    20          else:
    21              if (not os.path.isabs(fp)):
    22                  dfp = self.get_path(None, 'defaultPath')
    23                  fp = os.path.join(dfp, fp)
    24              self.fileh = file(fp, 'a')
    25          clen = self.get_setting(None, 'cacheLength')
    26          if clen:
    27              self.cacheLen = int(clen)
    28          else:
    29              self.cacheLen = 0
    30
    31 -    def log(self, session, txt):
    32          now = time.strftime("%Y-%m-%d %H:%M:%S")
    33          line = "[%s]: %s" % (now, txt)
    34          self._logLine(line)
    35
    36 -    def _logLine(self, line):
    37          if (self.lineCache and self.lineCache[-1].startswith(line)):
    38              self.lineCache[-1] += "."
    39          else:
    40              self.lineCache.append(line)
    41          if (len(self.lineCache) > self.cacheLen):
    42              for l in self.lineCache:
    43                  self.fileh.write(l + "\n")
    44              self.fileh.flush()
    45              self.lineCache = []
    46
    47
    48 -class FunctionLogger(SimpleLogger):
    49
    50 -    def __init__(self, session, config, parent):
    51          self.cacheLen = 1000
    52          SimpleLogger.__init__(self, session, config, parent)
    53
    54 -    def _myRepr(self, a):
    55          if (isinstance(a, C3Object)):
    56              return a.id
    57          elif (isinstance(a, Session)):
    58              return "Session(%s)" % (a.user)
    59          elif (isinstance(a, PrefixableObject)):
    60              return repr(a.toCQL())
    61          else:
    62              return repr(a)
    63
    64 -    def log(self, object, fn, *args, **kw):
    65
    66          frame = sys._getframe(2)
    67          caller = frame.f_code.co_name
    68          now = time.strftime("%Y-%m-%d %H:%M:%S")
    69          line = ["[%s]: from %s() : %s.%s(" % (now, caller, object.id, fn)]
    70          ln = []
    71          for a in args:
    72              ln.append(self._myRepr(a))
    73          for k in kw:
    74              ln.append("%s=%s" % (k, self._myRepr(kw[k])))
    75          atxt = ','.join(ln)
    76          line.append(atxt)
    77          line.append(")")
    78          line = ''.join(line)
    79
    80          self._logLine(line)
    81