mailman  2.1.39
About: Mailman 2 - The GNU Mailing List Management System.
  Fossies Dox: mailman-2.1.39.tgz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Syslog.py
Go to the documentation of this file.
1# Copyright (C) 1998-2018 by the Free Software Foundation, Inc.
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public License
5# as published by the Free Software Foundation; either version 2
6# of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
16# USA.
17
18"""Central logging class for the Mailman system.
19
20This might eventually be replaced by a syslog based logger, hence the name.
21"""
22
23import quopri
24
25from Mailman.Logging.StampedLogger import StampedLogger
26
27
28
29# Global, shared logger instance. All clients should use this object.
30syslog = None
31
32
33
34# Don't instantiate except below.
35class _Syslog:
36 def __init__(self):
37 self._logfiles = {}
38
39 def __del__(self):
40 self.close()
41
42 def write(self, kind, msg, *args, **kws):
43 self.write_ex(kind, msg, args, kws)
44
45 # We need this because SMTPDirect tries to pass in a special dict-like
46 # object, which is not a concrete dictionary. This is not allowed by
47 # Python's extended call syntax. :(
48 def write_ex(self, kind, msg, args=None, kws=None):
49 origmsg = msg
50 logf = self._logfiles.get(kind)
51 if not logf:
52 logf = self._logfiles[kind] = StampedLogger(kind)
53 try:
54 if args:
55 msg %= args
56 if kws:
57 msg %= kws
58 # It's really bad if exceptions in the syslogger cause other crashes
59 except Exception, e:
60 msg = 'Bad format "%s": %s: %s' % (origmsg, repr(e), e)
61 try:
62 logf.write(msg + '\n')
63 except UnicodeError:
64 # Python 2.4 may fail to write 8bit (non-ascii) characters
65 # Also, if msg is unicode with non-ascii, quopri.encodestring()
66 # will throw UnicodeEncodeError, so avoid that.
67 if isinstance(msg, unicode):
68 msg = msg.encode('iso-8859-1', 'replace')
69 logf.write(quopri.encodestring(msg) + '\n')
70
71 # For the ultimate in convenience
72 __call__ = write
73
74 def close(self):
75 for kind, logger in self._logfiles.items():
76 logger.close()
77 self._logfiles.clear()
78
79
80syslog = _Syslog()
def write(self, kind, msg, *args, **kws)
Definition: Syslog.py:42
def write_ex(self, kind, msg, args=None, kws=None)
Definition: Syslog.py:48