ewa.logutil

Package ewa :: Module logutil |
[hide private]
[frames] | no frames]

# Source Code for Module ewa.logutil

     1  import logging as _logging
     2  import logging.handlers as _loghandlers
     3  import sys as _sys
     4
     5  logger=_logging.getLogger('ewa')
     6  debug=logger.debug
     7  warn=logger.warn
     8  error=logger.error
     9  critical=logger.critical
    10  info=logger.info
    11  exception=logger.exception
    12
    13 -def initLogging(level=_logging.INFO,
    14                  format='%(levelname)s %(asctime)s %(filename)s | %(message)s',
    15                  stream=None,
    16                  filename=None,
    17                  datefmt=None,
    18                  rotate=None):
    19
    20      if isinstance(level, str):
    21          levels=dict((v,k) for k, v in _logging._levelNames.iteritems())
    22          level=levels.get(level, levels[level.upper()])
    23      if filename:
    24          if not rotate:
    25              handler=_logging.FileHandler(filename, 'a')
    26          elif rotate == True:
    27              handler=_loghandlers.RotatingFileHandler(filename,
    28                                                       mode='a',
    29                                                       maxBytes=1e7,
    30                                                       backupCount=10)
    31          elif isinstance(rotate, int):
    32              handler=_loghandlers.RotatingFileHandler(filename,
    33                                                       mode='a',
    34                                                       maxBytes=rotate,
    35                                                       backupCount=10)
    36          elif isinstance(rotate, tuple) and len(rotate==2):
    37              handler=_loghandlers.RotatingFileHandler(filename,
    38                                                       mode='a',
    39                                                       maxBytes=rotate[0],
    40                                                       backupCount=rotate[1])
    41          elif not isinstance(rotate, str):
    42              raise ValueError, "illegal value for logrotate: %s" % rotate
    43          else:
    44              #rotate can be two values, corresponding to "when" and
    45              #"interval" in logging.TimedRotatingHandler
    46              if ':' in rotate:
    47                  when, interval=rotate.split(':', 1)
    48                  interval=int(interval)
    49              else:
    50                  when=rotate
    51                  interval=1
    52              # special values
    53              if when=='weekly':
    54                  when='w0'
    55              if when=='daily':
    56                  when='d'
    57              handler=_loghandlers.TimedRotatingFileHandler(filename,
    58                                                            when=when,
    59                                                            interval=interval)
    60      else:
    61          stream=stream or _sys.stderr
    62          handler=_logging.StreamHandler(stream)
    63      formatter=_logging.Formatter(format, datefmt)
    64      handler.setFormatter(formatter)
    65      logger.addHandler(handler)
    66      logger.setLevel(level)
    67