diff options
author | Robey Pointer <robey@lag.net> | 2005-02-28 07:09:02 +0000 |
---|---|---|
committer | Robey Pointer <robey@lag.net> | 2005-02-28 07:09:02 +0000 |
commit | 8ee26cd41246ee90418c148a8a5cf9bf78f09e45 (patch) | |
tree | 62be0a24c978a9c033b450da977a75e5fb979dda | |
parent | e915dccc7dc97b79642a541da77c08c4c1e89b03 (diff) |
[project @ Arch-1:robey@lag.net--2003-public%secsh--dev--1.0--patch-149]
add thread ids to logs
add a logging filter that reports the thread-id of the logger, and use
that for all paramiko logging. since thread-local stuff didn't appear
until python 2.4, i hacked up my own little version to assign incrementing
numbers to threads as they log.
-rw-r--r-- | paramiko/channel.py | 6 | ||||
-rw-r--r-- | paramiko/sftp.py | 2 | ||||
-rw-r--r-- | paramiko/sftp_client.py | 2 | ||||
-rw-r--r-- | paramiko/sftp_server.py | 2 | ||||
-rw-r--r-- | paramiko/transport.py | 4 | ||||
-rw-r--r-- | paramiko/util.py | 30 |
6 files changed, 36 insertions, 10 deletions
diff --git a/paramiko/channel.py b/paramiko/channel.py index a09ddf72..0072a490 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -82,7 +82,7 @@ class Channel (object): self.out_buffer_cv = threading.Condition(self.lock) self.status_event = threading.Event() self.name = str(chanid) - self.logger = logging.getLogger('paramiko.chan.' + str(chanid)) + self.logger = util.get_logger('paramiko.chan.' + str(chanid)) self.pipe_rfd = self.pipe_wfd = None self.event = threading.Event() self.combine_stderr = False @@ -324,7 +324,7 @@ class Channel (object): @type name: str """ self.name = name - self.logger = logging.getLogger(self.transport.get_log_channel() + '.' + self.name) + self.logger = util.get_logger(self.transport.get_log_channel() + '.' + self.name) def get_name(self): """ @@ -825,7 +825,7 @@ class Channel (object): def _set_transport(self, transport): self.transport = transport - self.logger = logging.getLogger(self.transport.get_log_channel() + '.' + self.name) + self.logger = util.get_logger(self.transport.get_log_channel() + '.' + self.name) def _set_window(self, window_size, max_packet_size): self.in_window_size = window_size diff --git a/paramiko/sftp.py b/paramiko/sftp.py index c11564ad..ccf5b141 100644 --- a/paramiko/sftp.py +++ b/paramiko/sftp.py @@ -93,7 +93,7 @@ class SFTPError (Exception): class BaseSFTP (object): def __init__(self): - self.logger = logging.getLogger('paramiko.sftp') + self.logger = util.get_logger('paramiko.sftp') ### internals... diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index f40a536c..23826519 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -51,7 +51,7 @@ class SFTPClient (BaseSFTP): if type(sock) is Channel: # override default logger transport = self.sock.get_transport() - self.logger = logging.getLogger(transport.get_log_channel() + '.' + + self.logger = util.get_logger(transport.get_log_channel() + '.' + self.sock.get_name() + '.sftp') self.ultra_debug = transport.ultra_debug self._send_version() diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 318da02e..06fead36 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -58,7 +58,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): BaseSFTP.__init__(self) SubsystemHandler.__init__(self, channel, name) transport = channel.get_transport() - self.logger = logging.getLogger(transport.get_log_channel() + '.' + + self.logger = util.get_logger(transport.get_log_channel() + '.' + channel.get_name() + '.sftp') self.ultra_debug = transport.ultra_debug self.next_handle = 1 diff --git a/paramiko/transport.py b/paramiko/transport.py index b7a93c66..97026e80 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -243,7 +243,7 @@ class BaseTransport (threading.Thread): self.saved_exception = None self.clear_to_send = threading.Event() self.log_name = 'paramiko.transport' - self.logger = logging.getLogger(self.log_name) + self.logger = util.get_logger(self.log_name) # used for noticing when to re-key: self.received_bytes = 0 self.received_packets = 0 @@ -793,7 +793,7 @@ class BaseTransport (threading.Thread): @since: 1.1 """ self.log_name = name - self.logger = logging.getLogger(name) + self.logger = util.get_logger(name) def get_log_channel(self): """ diff --git a/paramiko/util.py b/paramiko/util.py index 0344919d..636b9f30 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -24,7 +24,7 @@ from __future__ import generators Useful functions used by the rest of paramiko. """ -import sys, struct, traceback +import sys, struct, traceback, threading from common import * # Change by RogerB - python < 2.3 doesn't have enumerate so we implement it @@ -188,6 +188,23 @@ def mod_inverse(x, m): u2 += m return u2 +g_thread_ids = {} +g_thread_counter = 0 +g_thread_lock = threading.Lock() +def get_thread_id(): + global g_thread_ids, g_thread_counter + tid = id(threading.currentThread()) + try: + return g_thread_ids[tid] + except KeyError: + g_thread_lock.acquire() + try: + g_thread_counter += 1 + ret = g_thread_ids[tid] = g_thread_counter + finally: + g_thread_lock.release() + return ret + def log_to_file(filename, level=DEBUG): "send paramiko logs to a logfile, if they're not already going somewhere" l = logging.getLogger("paramiko") @@ -196,6 +213,15 @@ def log_to_file(filename, level=DEBUG): l.setLevel(level) f = open(filename, 'w') lh = logging.StreamHandler(f) - lh.setFormatter(logging.Formatter('%(levelname)-.3s [%(asctime)s] %(name)s: %(message)s', + lh.setFormatter(logging.Formatter('%(levelname)-.3s [%(asctime)s] thr=%(_threadid)-3d %(name)s: %(message)s', '%Y%m%d-%H:%M:%S')) l.addHandler(lh) + +def get_logger(name): + l = logging.getLogger(name) + class PFilter (object): + def filter(self, record): + record._threadid = get_thread_id() + return True + l.addFilter(PFilter()) + return l |