Changeset 6399 for main/waeup.sirp/trunk


Ignore:
Timestamp:
17 Jun 2011, 15:06:26 (14 years ago)
Author:
uli
Message:
 
Location:
main/waeup.sirp/trunk/src/waeup/sirp/utils
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/logger.py

    r6376 r6399  
    196196        return
    197197
     198    def keys(self):
     199        return self._loggers.keys()
     200
    198201class ILogger(Interface):
    199202    """A logger cares for setup, update and restarting of a Python logger.
     
    304307        self._logger.setLevel(self.options.get('level', logging.DEBUG))
    305308        return self._logger
     309
     310#: A place where we store seen loggers.
     311SiteLoggersRepository = dict()
     312
     313class SiteLoggersFinder(object):
     314    grok.provides(ISiteLoggers)
     315
     316    def getLoggers(self, site=None):
     317        if site is None:
     318            site = grok.getSite()
     319        if site not in SiteLoggersRepository.keys():
     320            SiteLoggersRepository[site] = SiteLoggers()
     321        return SiteLoggersRepository[site]
     322
     323grok.global_utility(SiteLoggersFinder, provides=ISiteLoggers)
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/tests/test_logger.py

    r6371 r6399  
    2626import tempfile
    2727from zope.app.testing.functional import FunctionalTestCase
     28from zope.component import queryUtility
    2829from zope.component.hooks import setSite, clearSite
    2930from zope.interface.verify import verifyClass, verifyObject
     
    3132from waeup.sirp.testing import FunctionalLayer, doctestsuite_for_module
    3233from waeup.sirp.utils.logger import (
    33     ISiteLoggers, ILogger, SiteLoggers, WAeUPLogger)
    34 
    35 class FakeDatacenter(object):
    36     def __init__(self):
    37         self.storage = None
    38 
    39 class FakeSite(dict):
    40     def __init__(self):
    41         super(FakeSite, self).__init__()
    42         self.__name__ = None
    43         self['datacenter'] = FakeDatacenter()
    44 
     34    ISiteLoggers, ILogger, SiteLoggers, WAeUPLogger, SiteLoggersRepository)
    4535
    4636class SiteLoggersTests(FunctionalTestCase):
     
    6151
    6252        self.workdir = tempfile.mkdtemp()
     53        SiteLoggersRepository.clear()
    6354        return
    6455
     
    10091        loggers.unregister('sample')
    10192        self.assertEqual(loggers._loggers, {})
     93
     94    def test_utility_no_site(self):
     95        # make sure we get loggers even if no site is defined/set
     96        util = queryUtility(ISiteLoggers)
     97        loggers = util.getLoggers()
     98        assert util is not None
     99        assert loggers is not None
     100        assert None in SiteLoggersRepository.keys()
     101
     102    def test_utility(self):
     103        # make sute we get loggers for a certain site
     104        setSite(self.app)
     105        util = queryUtility(ISiteLoggers)
     106        loggers = util.getLoggers()
     107        assert util is not None
     108        assert loggers is not None
     109        assert self.app in SiteLoggersRepository.keys()
     110
     111    def test_utility_multiple(self):
     112        # make sure we get the same loggers for same site
     113        setSite(self.app)
     114        self.assertEqual(SiteLoggersRepository.keys(), [])
     115        util = queryUtility(ISiteLoggers)
     116        loggers1 = util.getLoggers()
     117        loggers2 = util.getLoggers()
     118        assert loggers1 is loggers2
     119
     120    def test_utility_given_site(self):
     121        # make sure a passed site is respected
     122        util = queryUtility(ISiteLoggers)
     123        loggers = util.getLoggers(site=self.app)
     124        assert loggers is not None
     125        assert self.app in SiteLoggersRepository.keys()
    102126
    103127class LoggerTests(FunctionalTestCase):
Note: See TracChangeset for help on using the changeset viewer.