From 50d63690eecc21896c1b210f5e097f854ed4571d Mon Sep 17 00:00:00 2001 From: Robey Pointer Date: Sat, 22 Mar 2008 19:36:16 -0700 Subject: [PATCH] [project @ robey@lag.net-20080323023616-fpo17kr0qxxbgthv] 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. --- paramiko/channel.py | 11 +++++------ paramiko/sftp.py | 6 +----- paramiko/sftp_client.py | 6 ++++-- paramiko/sftp_server.py | 8 +++++--- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/paramiko/channel.py b/paramiko/channel.py index b331fbf..c3fa9fe 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -87,7 +87,7 @@ class Channel (object): self.in_window_sofar = 0 self.status_event = threading.Event() self._name = str(chanid) - self.logger = util.get_logger('paramiko.chan.' + str(chanid)) + self.logger = util.get_logger('paramiko.transport') self._pipe = None self.event = threading.Event() self.combine_stderr = False @@ -394,14 +394,13 @@ class Channel (object): def set_name(self, 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. - @param name: new channel name. + @param name: new channel name @type name: str """ self._name = name - self.logger = util.get_logger(self.transport.get_log_channel() + '.' + self._name) def get_name(self): """ @@ -898,7 +897,7 @@ class Channel (object): def _set_transport(self, 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): self.in_window_size = window_size @@ -1062,7 +1061,7 @@ class Channel (object): 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): while True: diff --git a/paramiko/sftp.py b/paramiko/sftp.py index 417449d..dc8e99a 100644 --- a/paramiko/sftp.py +++ b/paramiko/sftp.py @@ -129,11 +129,7 @@ class BaseSFTP (object): return version 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): while len(out) > 0: diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index efb75ce..581d0a3 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -80,8 +80,7 @@ class SFTPClient (BaseSFTP): if type(sock) is Channel: # override default logger transport = self.sock.get_transport() - self.logger = util.get_logger(transport.get_log_channel() + '.' + - self.sock.get_name() + '.sftp') + self.logger = util.get_logger(transport.get_log_channel() + '.sftp') self.ultra_debug = transport.get_hexdump() try: server_version = self._send_version() @@ -105,6 +104,9 @@ class SFTPClient (BaseSFTP): chan.invoke_subsystem('sftp') return cls(chan) from_transport = classmethod(from_transport) + + def _log(self, level, msg): + super(SFTPClient, self)._log(level, "[chan " + self.sock.get_name() + "] " + msg) def close(self): """ diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 0e7473e..df7df62 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -66,15 +66,17 @@ class SFTPServer (BaseSFTP, SubsystemHandler): BaseSFTP.__init__(self) SubsystemHandler.__init__(self, channel, name, server) transport = channel.get_transport() - self.logger = util.get_logger(transport.get_log_channel() + '.' + - channel.get_name() + '.sftp') + self.logger = util.get_logger(transport.get_log_channel() + '.sftp') self.ultra_debug = transport.get_hexdump() self.next_handle = 1 # map of handle-string to SFTPHandle for files & folders: self.file_table = { } self.folder_table = { } 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): self.sock = channel self._log(DEBUG, 'Started sftp server on channel %s' % repr(channel))