bug 200416:
don't create a new logger for every channel and every sftp client or server.
it causes python to leak lots of useless logger objects, because they never
go away. instead, log the channel # in the message, and use only a couple of
standard log nodes.
This commit is contained in:
Robey Pointer 2008-03-22 19:36:16 -07:00
parent 7854d600a7
commit 50d63690ee
4 changed files with 15 additions and 16 deletions

View File

@ -87,7 +87,7 @@ class Channel (object):
self.in_window_sofar = 0 self.in_window_sofar = 0
self.status_event = threading.Event() self.status_event = threading.Event()
self._name = str(chanid) self._name = str(chanid)
self.logger = util.get_logger('paramiko.chan.' + str(chanid)) self.logger = util.get_logger('paramiko.transport')
self._pipe = None self._pipe = None
self.event = threading.Event() self.event = threading.Event()
self.combine_stderr = False self.combine_stderr = False
@ -394,14 +394,13 @@ class Channel (object):
def set_name(self, name): def set_name(self, name):
""" """
Set a name for this channel. Currently it's only used to set the name Set a name for this channel. Currently it's only used to set the name
of the log level used for debugging. The name can be fetched with the of the channel in logfile entries. The name can be fetched with the
L{get_name} method. L{get_name} method.
@param name: new channel name. @param name: new channel name
@type name: str @type name: str
""" """
self._name = name self._name = name
self.logger = util.get_logger(self.transport.get_log_channel() + '.' + self._name)
def get_name(self): def get_name(self):
""" """
@ -898,7 +897,7 @@ class Channel (object):
def _set_transport(self, transport): def _set_transport(self, transport):
self.transport = transport self.transport = transport
self.logger = util.get_logger(self.transport.get_log_channel() + '.' + self._name) self.logger = util.get_logger(self.transport.get_log_channel())
def _set_window(self, window_size, max_packet_size): def _set_window(self, window_size, max_packet_size):
self.in_window_size = window_size self.in_window_size = window_size
@ -1062,7 +1061,7 @@ class Channel (object):
def _log(self, level, msg, *args): def _log(self, level, msg, *args):
self.logger.log(level, msg, *args) self.logger.log(level, "[chan " + self._name + "] " + msg, *args)
def _wait_for_event(self): def _wait_for_event(self):
while True: while True:

View File

@ -129,10 +129,6 @@ class BaseSFTP (object):
return version return version
def _log(self, level, msg): def _log(self, level, msg):
if issubclass(type(msg), list):
for m in msg:
self.logger.log(level, m)
else:
self.logger.log(level, msg) self.logger.log(level, msg)
def _write_all(self, out): def _write_all(self, out):

View File

@ -80,8 +80,7 @@ class SFTPClient (BaseSFTP):
if type(sock) is Channel: if type(sock) is Channel:
# override default logger # override default logger
transport = self.sock.get_transport() transport = self.sock.get_transport()
self.logger = util.get_logger(transport.get_log_channel() + '.' + self.logger = util.get_logger(transport.get_log_channel() + '.sftp')
self.sock.get_name() + '.sftp')
self.ultra_debug = transport.get_hexdump() self.ultra_debug = transport.get_hexdump()
try: try:
server_version = self._send_version() server_version = self._send_version()
@ -106,6 +105,9 @@ class SFTPClient (BaseSFTP):
return cls(chan) return cls(chan)
from_transport = classmethod(from_transport) from_transport = classmethod(from_transport)
def _log(self, level, msg):
super(SFTPClient, self)._log(level, "[chan " + self.sock.get_name() + "] " + msg)
def close(self): def close(self):
""" """
Close the SFTP session and its underlying channel. Close the SFTP session and its underlying channel.

View File

@ -66,8 +66,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler):
BaseSFTP.__init__(self) BaseSFTP.__init__(self)
SubsystemHandler.__init__(self, channel, name, server) SubsystemHandler.__init__(self, channel, name, server)
transport = channel.get_transport() transport = channel.get_transport()
self.logger = util.get_logger(transport.get_log_channel() + '.' + self.logger = util.get_logger(transport.get_log_channel() + '.sftp')
channel.get_name() + '.sftp')
self.ultra_debug = transport.get_hexdump() self.ultra_debug = transport.get_hexdump()
self.next_handle = 1 self.next_handle = 1
# map of handle-string to SFTPHandle for files & folders: # map of handle-string to SFTPHandle for files & folders:
@ -75,6 +74,9 @@ class SFTPServer (BaseSFTP, SubsystemHandler):
self.folder_table = { } self.folder_table = { }
self.server = sftp_si(server, *largs, **kwargs) self.server = sftp_si(server, *largs, **kwargs)
def _log(self, level, msg):
super(SFTPServer, self)._log(level, "[chan " + self.sock.get_name() + "] " + msg)
def start_subsystem(self, name, transport, channel): def start_subsystem(self, name, transport, channel):
self.sock = channel self.sock = channel
self._log(DEBUG, 'Started sftp server on channel %s' % repr(channel)) self._log(DEBUG, 'Started sftp server on channel %s' % repr(channel))