Changeset 06b07f in indico


Ignore:
Timestamp:
08/31/10 07:47:46 (3 years ago)
Author:
Jose Benito <jose.benito.gonzalez@…>
Branches:
master, burotel, hello-world-walkthrough, ipv6, new-webex, v0.97-series, v0.98-series, v0.98.2, v0.98.3, v0.98b1, v0.98b2, v0.99, 051b2622c51afb171a1dedb46a0df4fbb0cbd02e, d9941f8582b36b24821a11ea5ba16fda6a457fb1
Children:
b40a31
Parents:
e73bb6
git-author:
Ian Rolewicz <ian.rolewicz@…> (05/25/10 16:15:40)
git-committer:
Jose Benito <jose.benito.gonzalez@…> (08/31/10 07:47:46)
Message:

[IMP] Moved config of logging to separate file

  • used the mechanism of the logging module
  • added file logging.conf.sample
  • All the logging parameters can now be modified from the logging.conf file.
  • If logging.conf is not set by the user, default values taken from indico.conf are set by default for the available handlers.
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • indico/MaKaC/common/logger.py

    rfb9bd3 r06b07f  
    1 import os, logging 
     1import os, logging.config 
    22import logging.handlers 
    33 
    44from MaKaC.common.Configuration import Config 
     5import ConfigParser 
    56 
    67class ExtraIndicoFilter(logging.Filter): 
     
    1112        return 1 
    1213 
     14class LoggerUtils: 
     15 
     16    @classmethod 
     17    def configFromFile(self, fname, defaultArgs, filters): 
     18        """ 
     19        Read the logging configuration from the logging.conf file. 
     20        Fetch default values if the logging.conf file is not set. 
     21        """ 
     22 
     23        if not os.path.exists(fname): 
     24            return 
     25 
     26        cp = ConfigParser.ConfigParser() 
     27        if hasattr(cp, 'readfp') and hasattr(fname, 'readline'): 
     28            cp.readfp(fname) 
     29        else: 
     30            cp.read(fname) 
     31 
     32        formatters = logging.config._create_formatters(cp) 
     33 
     34        logging._acquireLock() 
     35        try: 
     36            logging._handlers.clear() 
     37            del logging._handlerList[:] 
     38            handlers = self._install_handlers(cp, defaultArgs, formatters, filters) 
     39            try: 
     40                logging.config._install_loggers(cp, handlers) 
     41            except TypeError: 
     42                logging.config._install_loggers(cp, handlers, False) 
     43 
     44        finally: 
     45            logging._releaseLock() 
     46 
     47    @classmethod 
     48    def _install_handlers(self, cp, defaultArgs, formatters, filters = None): 
     49        """ 
     50        Install and return handlers. If a handler configuration 
     51        is missing its args, fetches the default values from the 
     52        indico.conf file 
     53        """ 
     54        hlist = cp.get("handlers", "keys") 
     55        if not len(hlist): 
     56            return {} 
     57        hlist = hlist.split(",") 
     58        handlers = {} 
     59        fixups = [] #for inter-handler references 
     60 
     61        for hand in hlist: 
     62            sectname = "handler_%s" % hand.strip() 
     63            klass = cp.get(sectname, "class") 
     64            opts = cp.options(sectname) 
     65            if "formatter" in opts: 
     66                fmt = cp.get(sectname, "formatter") 
     67            else: 
     68                fmt = "" 
     69            klass = eval(klass, vars(logging)) 
     70            if "args" in opts : 
     71                # if the args are not present in the file, 
     72                # take default values 
     73                args = cp.get(sectname, "args") 
     74            else : 
     75                try: 
     76                    args = defaultArgs[hand.strip()] 
     77                except KeyError: 
     78                    continue 
     79            args = eval(args, vars(logging)) 
     80            h = apply(klass, args) 
     81            if "level" in opts: 
     82                level = cp.get(sectname, "level") 
     83                h.setLevel(logging._levelNames[level]) 
     84            if len(fmt): 
     85                h.setFormatter(formatters[fmt]) 
     86            if filters and hand.strip() in filters: 
     87                for fltr in filters[hand.strip()]: 
     88                    h.addFilter(fltr) 
     89            #temporary hack for FileHandler and MemoryHandler. 
     90            if klass == logging.handlers.MemoryHandler: 
     91                if "target" in opts: 
     92                    target = cp.get(sectname,"target") 
     93                else: 
     94                    target = "" 
     95                if len(target): #the target handler may not be loaded yet, so keep for later... 
     96                    fixups.append((h, target)) 
     97            handlers[hand] = h 
     98        #now all handlers are loaded, fixup inter-handler references... 
     99        for h, t in fixups: 
     100            h.setTarget(handlers[t]) 
     101        return handlers 
     102 
    13103class Logger: 
    14104    """ 
     
    18108    config = Config.getInstance() 
    19109 
    20     __rootLogger = logging.getLogger('') 
    21  
    22     __indicoFileHandler = logging.FileHandler(os.path.join(config.getLogDir(), 'indico.log'),'a') 
    23     __otherFileHandler = logging.FileHandler(os.path.join(config.getLogDir(), 'other.log'),'a') 
    24  
    25     __formatter = logging.Formatter('%(asctime)s %(name)-16s: %(levelname)-8s %(message)s') 
    26     __indicoFileHandler.setFormatter(__formatter) 
    27     __otherFileHandler.setFormatter(__formatter) 
    28     __indicoFileHandler.addFilter(logging.Filter('indico')) 
    29     __otherFileHandler.addFilter(ExtraIndicoFilter()) 
    30  
     110    configDir = config.getLogDir() 
     111    smtpServer = config.getSmtpServer() 
    31112    serverName = config.getWorkerName() 
    32113    if not serverName: 
    33114        serverName = config.getHostNameURL() 
    34115 
    35     # TODO: add config option to disable this? 
    36     __smtpHandler = logging.handlers.SMTPHandler(config.getSmtpServer(), 
    37                                             'logger@%s' % serverName, 
    38                                             config.getSupportEmail(), 
    39                                             'Unexpected Exception occurred at %s' % serverName) 
     116    # Default arguments for the handlers, taken mostly for the configuration 
     117    defaultArgs = { 'indico' : "('%s', 'a')" %os.path.join(configDir, 'indico.log'), 
     118                    'other'  : "('%s', 'a')" %os.path.join(configDir, 'other.log'), 
     119                    'smtp'   : "('%s', 'logger@%s', ['%s'], 'Unexpected Exception occurred at %s')" 
     120                                %(smtpServer, serverName, config.getSupportEmail(), serverName) 
     121                    } 
    40122 
    41     __smtpHandler.addFilter(logging.Filter('indico')) 
    42     __smtpHandler.setLevel(logging.ERROR) 
    43     __smtpHandler.setFormatter(logging.Formatter("%(asctime)s %(name)s - %(levelname)s %(filename)s:%(lineno)d\n\n%(message)s")) 
     123    # Lists of filters for each handler 
     124    filters = {'indico' : [logging.Filter('indico')], 
     125               'other'  : [ExtraIndicoFilter()], 
     126               'smtp'   : [logging.Filter('indico')]} 
    44127 
    45     __rootLogger.addHandler(__indicoFileHandler) 
    46     __rootLogger.addHandler(__otherFileHandler) 
    47     __otherFileHandler.setLevel(logging.WARNING) 
    48     __rootLogger.addHandler(__smtpHandler) 
     128    logConfFilepath = os.path.join(config.getConfigurationDir(), "logging.conf") 
    49129 
    50     # TODO: see savannah task 
    51     # if it's debug mode, be more exhaustive 
    52     #if DEBUG or HelperMaKaCInfo.getMaKaCInfoInstance().isDebugActive(): 
    53     #    __rootLogger.setLevel(logging.DEBUG) 
    54     #else: 
    55     #    __rootLogger.setLevel(logging.WARNING) 
    56  
    57     __rootLogger.setLevel(logging.DEBUG) 
     130    #logging.config.fileConfig(logConfFilepath) 
     131    LoggerUtils.configFromFile(logConfFilepath, defaultArgs, filters) 
    58132 
    59133    @classmethod 
  • indico/MaKaC/common/mail.py

    rbdd862 r06b07f  
    2626from MaKaC.common.logger import Logger 
    2727 
    28 from MaKaC.common.logger import Logger 
    29  
    3028class GenericMailer: 
    3129 
     
    5149            if len(cc) == 0 : 
    5250                notification.getCCList().remove(cc) 
    53          
     51 
    5452        to=", ".join(notification.getToList()) 
    5553#        raise to 
     
    7472 
    7573        Logger.get('mail').debug('Mail sent to %s' % to) 
    76          
     74 
    7775    def sendAndLog(notification, conference, module="", user = None): 
    7876        GenericMailer.send(notification) 
     
    8684        except: 
    8785            logData["toList"] = notification.getToList() 
    88         logData["ccList"] = notification._ccList  
     86        logData["ccList"] = notification._ccList 
    8987        try: 
    9088            logData["subject"] = notification._subject 
  • indico/MaKaC/consoleScripts/installBase.py

    r89b48e r06b07f  
    514514    # copy the db config files 
    515515    for f in [xx for xx in ('%s/zdctl.conf' % targetDirs['etc'], 
    516                             '%s/zodb.conf' % targetDirs['etc']) if not os.path.exists(xx)]: 
     516                            '%s/zodb.conf' % targetDirs['etc'], 
     517                            '%s/logging.conf' %targetDirs['etc']) if not os.path.exists(xx)]: 
    517518        shutil.copy('%s.sample' % f, f) 
    518519 
  • indico/MaKaC/plugins/RoomBooking/default/dalManager.py

    rbdd862 r06b07f  
    5050        else: 
    5151            return False 
    52           
     52 
    5353    @staticmethod 
    5454    def theInstance(): 
     
    7373    @staticmethod 
    7474    def getRoot(name=""): 
     75 
    7576        if name == "": 
    7677            return DALManager.root 
     
    9192        else: 
    9293            raise MaKaCError("Cannot connect to the room booking database") 
    93              
     94 
    9495    @staticmethod 
    9596    def connect(): 
     
    100101                DALManager.connection = DALManager.theInstance().db.open() 
    101102            DALManager.root = DALManager.connection.root() 
    102          
     103 
    103104    @staticmethod 
    104105    def disconnect(): 
     
    109110        DALManager.root = None 
    110111        DALManager.connection = None 
    111      
     112 
    112113    @staticmethod 
    113114    def commit(): 
     
    116117        if DALManager.isConnected(): 
    117118            DALManager.connection.transaction_manager.get().commit() 
    118          
     119 
    119120    @staticmethod 
    120121    def rollback(): 
     
    136137            return 
    137138        DALManager.theInstance().db.pack(days=days) 
    138          
     139 
  • setup.py

    r89b48e r06b07f  
    245245            shutil.copy('etc/indico.conf.sample', local) 
    246246 
    247         for f in [x for x in ('etc/zdctl.conf', 'etc/zodb.conf') if not os.path.exists(x)]: 
     247        for f in [x for x in ('etc/zdctl.conf', 'etc/zodb.conf', 'etc/logging.conf') if not os.path.exists(x)]: 
    248248            shutil.copy('%s.sample' % f, f) 
    249249 
Note: See TracChangeset for help on using the changeset viewer.