Changeset 4874 for waeup/trunk/src


Ignore:
Timestamp:
22 Jan 2010, 12:49:57 (15 years ago)
Author:
uli
Message:

Create an application logger.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/trunk/src/waeup/app.py

    r4789 r4874  
    11import grok
     2import logging
     3import os
    24from zope.app.authentication.authentication import PluggableAuthentication
    35from zope.app.security.interfaces import IAuthentication
     
    1820        PluggableAuthentication, provides = IAuthentication,
    1921        setup = setup_authentication)
     22
     23    @property
     24    def logger(self):
     25        sitename = self.__name__
     26        loggername = 'waeup.sirp.%s' % sitename
     27        logger = logging.getLogger(loggername)
     28        if not logger.handlers:
     29            logger = self._setupLogger(logger)
     30        return logger
     31       
    2032   
    2133    def __init__(self, name=u'Sample University', **kw):
     
    3042        self['users'] = UserContainer()
    3143        self['datacenter'] = DataCenter()
     44
     45    def _setupLogger(self, logger):
     46        """Setup general application logger.
     47
     48        The logfile will be stored in the datacenter logs/ dir.
     49        """
     50        logdir = os.path.join(self['datacenter'].storage, 'logs')
     51        if not os.path.exists(logdir):
     52            os.mkdir(logdir)
     53        filename = os.path.join(logdir, 'application.log')
     54
     55        # Create a rotating file handler logger for application.
     56        handler = logging.handlers.RotatingFileHandler(
     57            filename, maxBytes=5*1024**1, backupCount=7)
     58        formatter = logging.Formatter(
     59            '%(asctime)s - %(levelname)s - %(message)s')
     60        handler.setFormatter(formatter)
     61       
     62        # Don't send log msgs to ancestors. This stops displaying
     63        # logmessages on the commandline.
     64        logger.propagate = False
     65        logger.addHandler(handler)
     66        return logger
Note: See TracChangeset for help on using the changeset viewer.