Changeset 6445
- Timestamp:
- 21 Jun 2011, 01:00:45 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/utils/logger.py
r6440 r6445 26 26 import grok 27 27 import logging 28 from zope.interface import Interface, Attribute 28 29 29 30 marker_ = object() 30 31 def setup_logger(loggername, filename, site=marker_, logdir=marker_,32 handler=marker_, formatter=marker_, propagate=False,33 level=logging.DEBUG):34 """Create a logger.35 """36 if site is marker_:37 site = grok.getSite()38 if '%s' in loggername:39 name = getattr(site, '__name__', 'app')40 loggername = loggername % (name,)41 logger = logging.getLogger(loggername)42 update_logger(logger, filename, site=site, logdir=logdir, handler=handler,43 formatter=formatter, propagate=propagate, level=level)44 return logger, loggername45 46 def update_logger(logger, filename, site=marker_, logdir=marker_,47 handler=marker_, formatter=marker_, propagate=False,48 level=logging.DEBUG):49 if site is marker_:50 site = grok.getSite()51 if logdir is marker_:52 logdir = os.path.join(site['datacenter'].storage, 'logs')53 if not os.path.exists(logdir):54 os.mkdir(logdir)55 filename = os.path.join(logdir, filename)56 57 # Create a rotating file handler logger for datacenter.58 if handler is marker_:59 handler = logging.handlers.RotatingFileHandler(60 filename, maxBytes=5*1024**1, backupCount=5)61 if formatter is marker_:62 formatter = logging.Formatter(63 '%(asctime)s - %(levelname)s - %(message)s')64 handler.setFormatter(formatter)65 66 logger.propagate = propagate67 logger.setLevel(level)68 logger.addHandler(handler)69 return logger70 71 def get_logger(loggername, filename, site=marker_, **kw):72 """Get a logger.73 """74 if '%s' in loggername:75 if site is marker_:76 site = grok.getSite()77 loggername = loggername % (78 getattr(site, '__name__', 'app'))79 logger = logging.getLogger(loggername)80 if not logger.handlers:81 kw.update(dict(site=site))82 logger, loggername = setup_logger(83 loggername, filename, **kw)84 return logger85 31 86 32 class WAeUPLogger(object): … … 117 63 if self.site is marker_: 118 64 self.site = grok.getSite() 119 if '%s' in loggername:120 name = getattr(s ite, '__name__', '__app')121 loggername =loggername % (name,)122 self.logger = logging.getLogger( loggername)65 if '%s' in self.loggername: 66 name = getattr(self.site, '__name__', '__app') 67 self.loggername = self.loggername % (name,) 68 self.logger = logging.getLogger(self.loggername) 123 69 self.update() 124 70 return … … 128 74 self.site = grok.getSite() 129 75 if self.logdir is marker_: 130 self.logdir = os.path.join(s ite['datacenter'].storage, 'logs')76 self.logdir = os.path.join(self.site['datacenter'].storage, 'logs') 131 77 if not os.path.exists(self.logdir): 132 78 os.mkdir(self.logdir) … … 137 83 self.handlers = [logging.handlers.RotatingFileHandler( 138 84 filename, maxBytes=5*1024**1, backupCount=5)] 139 if formatters is marker_:140 formatters = [logging.Formatter(85 if self.formatters is marker_: 86 self.formatters = [logging.Formatter( 141 87 '%(asctime)s - %(levelname)s - %(message)s')] 142 88 … … 150 96 return self.logger 151 97 152 153 from zope import schema154 from zope.interface import Interface, Attribute155 98 156 99 class ISiteLoggers(Interface):
Note: See TracChangeset for help on using the changeset viewer.