1 | ## |
---|
2 | ## logging.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Mon Jun 13 01:25:07 2011 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 Uli Fouquet |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | """ |
---|
23 | Convenience stuff for logging. |
---|
24 | """ |
---|
25 | import os |
---|
26 | import grok |
---|
27 | import logging |
---|
28 | from zope.interface import Interface, Attribute |
---|
29 | from waeup.sirp.interfaces import ISiteLoggers, ILogger |
---|
30 | |
---|
31 | class SiteLoggers(object): |
---|
32 | |
---|
33 | grok.implements(ISiteLoggers) |
---|
34 | |
---|
35 | @property |
---|
36 | def loggers(self): |
---|
37 | return self._loggers.values() |
---|
38 | |
---|
39 | def __init__(self): |
---|
40 | self._loggers = dict() |
---|
41 | |
---|
42 | def getLogger(self, filename): |
---|
43 | waeup_logger = self._loggers.get(filename, None) |
---|
44 | return waeup_logger.logger |
---|
45 | |
---|
46 | def register(self, loggername, filename=None, site=None, **kw): |
---|
47 | if loggername in self._loggers.keys(): |
---|
48 | raise KeyError('Logger "%s" exists already.' % loggername) |
---|
49 | self._loggers[loggername] = WAeUPLogger( |
---|
50 | loggername, filename=filename, site=site, **kw) |
---|
51 | return |
---|
52 | |
---|
53 | def unregister(self, name): |
---|
54 | if name in self._loggers.keys(): |
---|
55 | del self._loggers[name] |
---|
56 | return |
---|
57 | |
---|
58 | def keys(self): |
---|
59 | return self._loggers.keys() |
---|
60 | |
---|
61 | class WAeUPLogger(object): |
---|
62 | |
---|
63 | grok.implements(ILogger) |
---|
64 | |
---|
65 | _logger = None |
---|
66 | |
---|
67 | def __init__(self, name, filename=None, site=None, **options): |
---|
68 | self.name = name |
---|
69 | self.filename = filename |
---|
70 | if filename is None: |
---|
71 | if '.' in name: |
---|
72 | name = name.rsplit('.', 1)[1] |
---|
73 | self.filename = "%s.log" % name |
---|
74 | self.site = site |
---|
75 | self.max_bytes = options.get('maxBytes', 5 * 1024 ** 2) |
---|
76 | self.backup_count = options.get('backupCount', 5) |
---|
77 | self.options = options |
---|
78 | return |
---|
79 | |
---|
80 | @property |
---|
81 | def logger(self): |
---|
82 | if self._needSetup(): |
---|
83 | self._setup() |
---|
84 | return logging.getLogger(self._loggername) |
---|
85 | |
---|
86 | def setup(self): |
---|
87 | return self._setup() |
---|
88 | |
---|
89 | def update(self, **options): |
---|
90 | self._logger = None # unset to be set up again on next access. |
---|
91 | # XXX: set options |
---|
92 | pass |
---|
93 | |
---|
94 | def _needSetup(self): |
---|
95 | if self._logger is None: |
---|
96 | return True |
---|
97 | if not '__app' in self._logger.name: |
---|
98 | return False |
---|
99 | if getattr(self.site, '__name__', None) is not None: |
---|
100 | return True |
---|
101 | return False |
---|
102 | |
---|
103 | def _removeHandlers(self): |
---|
104 | handlers = self._logger.handlers |
---|
105 | for handler in handlers: |
---|
106 | self._logger.removeHandler(handler) |
---|
107 | |
---|
108 | def _setup(self): |
---|
109 | if self.site is None: |
---|
110 | self.site = grok.getSite() |
---|
111 | self._loggername = self.name |
---|
112 | if not '.' in self._loggername: |
---|
113 | self._loggername = 'waeup.sirp.%s.%s' % ('%s', self._loggername) |
---|
114 | if '%s' in self._loggername: |
---|
115 | site_name = getattr(self.site, '__name__', '__app') |
---|
116 | if site_name is None: |
---|
117 | site_name = '__app' |
---|
118 | self._loggername = self._loggername % (site_name,) |
---|
119 | self._logger = logging.getLogger(self._loggername) |
---|
120 | return self._update() |
---|
121 | |
---|
122 | def _update(self): |
---|
123 | self._removeHandlers() # Remove any old handlers |
---|
124 | self._logdir = os.path.join(self.site['datacenter'].storage, 'logs') |
---|
125 | if not os.path.exists(self._logdir): |
---|
126 | os.mkdir(self._logdir) |
---|
127 | filename = os.path.join(self._logdir, self.filename) |
---|
128 | |
---|
129 | # Create a rotating file handler logger for datacenter. |
---|
130 | default_handler = logging.handlers.RotatingFileHandler( |
---|
131 | filename, maxBytes=self.max_bytes, backupCount=self.backup_count) |
---|
132 | self.handler = self.options.get('handler', default_handler) |
---|
133 | |
---|
134 | default_formatter = logging.Formatter( |
---|
135 | '%(asctime)s - %(levelname)s - %(message)s') |
---|
136 | formatter = self.options.get('formatter', default_formatter) |
---|
137 | self.handler.setFormatter(formatter) |
---|
138 | |
---|
139 | self._logger.addHandler(self.handler) |
---|
140 | self._logger.propagate = self.options.get('propagate', False) |
---|
141 | self._logger.setLevel(self.options.get('level', logging.DEBUG)) |
---|
142 | return self._logger |
---|
143 | |
---|
144 | #: A place where we store seen loggers. |
---|
145 | SiteLoggersRepository = dict() |
---|
146 | |
---|
147 | class SiteLoggersFinder(object): |
---|
148 | grok.provides(ISiteLoggers) |
---|
149 | |
---|
150 | def getLoggers(self, site=None): |
---|
151 | if site is None: |
---|
152 | site = grok.getSite() |
---|
153 | if site not in SiteLoggersRepository.keys(): |
---|
154 | SiteLoggersRepository[site] = SiteLoggers() |
---|
155 | return SiteLoggersRepository[site] |
---|
156 | |
---|
157 | grok.global_utility(SiteLoggersFinder, provides=ISiteLoggers) |
---|