[Zope-CVS] CVS: Packages/SFTPGateway/src/sftpgateway - application.py:1.15

Fred L. Drake, Jr. fred at zope.com
Wed Jun 23 23:57:37 EDT 2004


Update of /cvs-repository/Packages/SFTPGateway/src/sftpgateway
In directory cvs.zope.org:/tmp/cvs-serv26845/src/sftpgateway

Modified Files:
	application.py 
Log Message:
Avoid leaking a logging.Logger object for each connection.

Loggers created by logging.getLogger() are kept alive in a global
table within the logging pakcage.  To avoid this, use a wrapper object
around a single logger that adds the annotations this had been abusing
the logger's "name" attribute for.

This causes a difference in the actual log; the specific difference is
determined by the message format configured for the log handlers.


=== Packages/SFTPGateway/src/sftpgateway/application.py 1.14 => 1.15 ===
--- Packages/SFTPGateway/src/sftpgateway/application.py:1.14	Thu Jan 29 10:55:22 2004
+++ Packages/SFTPGateway/src/sftpgateway/application.py	Wed Jun 23 23:57:07 2004
@@ -19,8 +19,6 @@
 import pwd
 import sys
 
-import ZConfig
-
 from twisted.conch.ssh import connection, factory, keys, session, userauth
 from twisted.internet import reactor
 from twisted.python import log
@@ -75,7 +73,7 @@
         count = self._child_logger_info.get(name, 0)
         count += 1
         self._child_logger_info[name] = count
-        return logging.getLogger("%s:%d" % (name, count))
+        return ConnectionLogger(self.logger, "%s:%d " % (name, count))
 
     # get_group() and get_user() only work properly when the FTP server
     # and the local machine share the same UID and GID mappings; it should
@@ -98,6 +96,54 @@
                 return pwd.getpwnam(name).pw_uid
             except KeyError:
                 return -1
+
+
+class ConnectionLogger(object):
+    """Connection-specific logger object.
+
+    This provides a way to add some specific information to each log
+    message without generated for a connection without creating a
+    separately-managed logger object using the logging module.
+    Managed logger objects are never discarded (since they're stored
+    in a global table in the logging module), but these are discarded
+    when the corresponding connection object is discarded.
+
+    It is unfortunate that this much machinery is needed to avoid this
+    sort of simple memory leak.
+
+    """
+    __slots__ = "__logger", "__prefix"
+
+    def __init__(self, logger, prefix):
+        if "%" in prefix:
+            raise ValueError("message prefix may not contain '%'")
+        self.__logger = logger
+        self.__prefix = prefix
+
+    def debug(self, msg, *args, **kw):
+        self.__logger.debug(self.__prefix + msg, *args, **kw)
+
+    def info(self, msg, *args, **kw):
+        self.__logger.info(self.__prefix + msg, *args, **kw)
+
+    def warning(self, msg, *args, **kw):
+        self.__logger.warning(self.__prefix + msg, *args, **kw)
+
+    warn = warning
+
+    def error(self, msg, *args, **kw):
+        self.__logger.error(self.__prefix + msg, *args, **kw)
+
+    def exception(self, msg, *args):
+        self.__logger.exception(self.__prefix + msg, *args)
+
+    def critical(self, msg, *args, **kw):
+        self.__logger.critical(self.__prefix + msg, *args, **kw)
+
+    fatal = critical
+
+    def log(self, level, msg, *args, **kw):
+        self.__logger.log(level, self.__prefix + msg, *args, **kw)
 
 
 class SFTPConnection(connection.SSHConnection):



More information about the Zope-CVS mailing list