Package buildxml :: Package xmlgetter :: Module log
[hide private]
[frames] | no frames]

Source Code for Module buildxml.xmlgetter.log

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  """ 
  5  This module contains classes for logging. 
  6   
  7  @author: Johannes Schwenk 
  8  @copyright: 2010, Johannes Schwenk 
  9  @version: 1.0 
 10  @date: 2010-09-15 
 11   
 12   
 13  """ 
 14   
 15  import sys 
 16   
 17  # Imortant! 
 18  reload(sys) 
 19  sys.setdefaultencoding('utf-8') 
 20   
 21  import logging 
 22  import logging.handlers 
 23   
 24  from config import LOG_FILENAME, LOG_LEVEL, LOG_ROLLOVER_SIZE, LOG_BACKUP_COUNT 
25 26 27 -class BaseLogger:
28 """ 29 This is the base class for all classes that want to use the logging 30 system. The class has only one public property L{logger}, which returns a 31 C{logging.Logger} instance. The filename of the log is defined in 32 L{config.LOG_FILENAME}, the logging level in L{config.LOG_LEVEL} and 33 the rollover size of the logfile in L{config.LOG_ROLLOVER_SIZE}. 34 35 """ 36 37 _loggers = {} 38 """ 39 Stores all instances of C{logging.Logger} that are created by the property 40 function L{logger} with their name as key. 41 42 @type: dict 43 44 """ 45 46 47 _source_name = None 48 """ 49 @ivar: The value of this variable will be used in the loggers names. 50 @type: string 51 52 """ 53 54
55 - def __init__(self, source_name=None):
56 """ 57 Initialize the BaseLogger. 58 59 @param source_name: If not C{None}, L{self._source_name} will be set 60 to its value. 61 @type source_name: string 62 63 """ 64 if source_name: 65 self._source_name = source_name
66 67 68 @property
69 - def logger(self):
70 """ 71 Returns a C{logging.Logger} instance. 72 73 If the is an instance variable C{_source_name}, use the classname 74 and C{self._source_name} as logger name, otherwise use only the 75 classname. 76 77 The C{Logger} instances will be stored in the L{_loggers} dictionary 78 by their name. A new instance will only be created if no logger 79 with that name already exists in the dictionary. 80 81 @return: A Logger that logs to L{LOG_FILENAME} . 82 @rtype: C{logging.Logger} 83 84 """ 85 # If there is a source_name, include it in the output. 86 if hasattr(self, u'_source_name'): 87 lname = u'[%s, %s]' % (self.__class__, self._source_name) 88 else: 89 lname = u'[%s]' % self.__class__ 90 # Prevent multiple loggers of the same name. 91 if not lname in self._loggers.keys(): 92 BaseLogger._loggers[lname] = self._getLogger(lname) 93 return BaseLogger._loggers[lname]
94 95
96 - def _getLogger(self, logger_name):
97 """ 98 Creates a C{logging.Logger} instance with the logging parameters 99 taken from L{config}. The L{LOG_LEVEL} specifies which messages get 100 logged to L{LOG_FILENAME}. If L{LOG_ROLLOVER_SIZE} is > 0, then 101 L{LOG_BACKUP_COUNT} copies of old logfiles with size 102 L{LOG_ROLLOVER_SIZE} will be kept. 103 104 @return: The newly created logger. 105 @rtype: C{logging.Logger} 106 107 """ 108 logger = logging.getLogger(logger_name) 109 logger.setLevel(LOG_LEVEL) 110 handler = logging.handlers.RotatingFileHandler( 111 LOG_FILENAME, maxBytes=LOG_ROLLOVER_SIZE, 112 backupCount=LOG_BACKUP_COUNT) 113 # Format the message. Be more verbose for the DEBUG level. 114 if LOG_LEVEL == logging.DEBUG: 115 formatter = logging.Formatter(u'%(asctime)s - %(levelname)s ' + \ 116 u'- %(name)s - [%(filename)s, line %(lineno)d]' + \ 117 u' - %(message)s') 118 else: 119 formatter = logging.Formatter(u'%(asctime)s - %(levelname)s ' + \ 120 u'- %(name)s - %(message)s') 121 handler.setFormatter(formatter) 122 logger.addHandler(handler) 123 return logger
124