THRIFT-2663: py - per module logger used in python lib
Client: Python-Library
Patch: Dustin Spicuzza
This enables per module log message filtering
This closes #187
diff --git a/lib/py/src/server/TNonblockingServer.py b/lib/py/src/server/TNonblockingServer.py
index fa478d0..4a035b6 100644
--- a/lib/py/src/server/TNonblockingServer.py
+++ b/lib/py/src/server/TNonblockingServer.py
@@ -29,7 +29,9 @@
import Queue
import select
import struct
+
import logging
+logger = logging.getLogger(__name__)
from thrift.transport import TTransport
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
@@ -54,7 +56,7 @@
processor.process(iprot, oprot)
callback(True, otrans.getvalue())
except Exception:
- logging.exception("Exception while processing request")
+ logger.exception("Exception while processing request")
callback(False, '')
WAIT_LEN = 0
@@ -116,18 +118,18 @@
# if we read 0 bytes and self.message is empty, then
# the client closed the connection
if len(self.message) != 0:
- logging.error("can't read frame size from socket")
+ logger.error("can't read frame size from socket")
self.close()
return
self.message += read
if len(self.message) == 4:
self.len, = struct.unpack('!i', self.message)
if self.len < 0:
- logging.error("negative frame size, it seems client "
+ logger.error("negative frame size, it seems client "
"doesn't use FramedTransport")
self.close()
elif self.len == 0:
- logging.error("empty frame, it's really strange")
+ logger.error("empty frame, it's really strange")
self.close()
else:
self.message = ''
@@ -145,7 +147,7 @@
elif self.status == WAIT_MESSAGE:
read = self.socket.recv(self.len - len(self.message))
if len(read) == 0:
- logging.error("can't read frame from socket (get %d of "
+ logger.error("can't read frame from socket (get %d of "
"%d bytes)" % (len(self.message), self.len))
self.close()
return
diff --git a/lib/py/src/server/TProcessPoolServer.py b/lib/py/src/server/TProcessPoolServer.py
index 7369466..eefb623 100644
--- a/lib/py/src/server/TProcessPoolServer.py
+++ b/lib/py/src/server/TProcessPoolServer.py
@@ -19,6 +19,8 @@
import logging
+logger = logging.getLogger(__name__)
+
from multiprocessing import Process, Value, Condition, reduction
from TServer import TServer
@@ -62,7 +64,7 @@
except (KeyboardInterrupt, SystemExit):
return 0
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
def serveClient(self, client):
"""Process input/output from a client for as long as possible"""
@@ -77,7 +79,7 @@
except TTransportException, tx:
pass
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
itrans.close()
otrans.close()
@@ -98,7 +100,7 @@
w.start()
self.workers.append(w)
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
# wait until the condition is set by stop()
while True:
@@ -109,7 +111,7 @@
except (SystemExit, KeyboardInterrupt):
break
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
self.isRunning.value = False
diff --git a/lib/py/src/server/TServer.py b/lib/py/src/server/TServer.py
index 976a8f3..00fd8cf 100644
--- a/lib/py/src/server/TServer.py
+++ b/lib/py/src/server/TServer.py
@@ -18,12 +18,14 @@
#
import Queue
-import logging
import os
import sys
import threading
import traceback
+import logging
+logger = logging.getLogger(__name__)
+
from thrift.Thrift import TProcessor
from thrift.protocol import TBinaryProtocol
from thrift.transport import TTransport
@@ -87,7 +89,7 @@
except TTransport.TTransportException, tx:
pass
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
itrans.close()
otrans.close()
@@ -113,7 +115,7 @@
except KeyboardInterrupt:
raise
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
def handle(self, client):
itrans = self.inputTransportFactory.getTransport(client)
@@ -126,7 +128,7 @@
except TTransport.TTransportException, tx:
pass
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
itrans.close()
otrans.close()
@@ -152,7 +154,7 @@
client = self.clients.get()
self.serveClient(client)
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
def serveClient(self, client):
"""Process input/output from a client for as long as possible"""
@@ -166,7 +168,7 @@
except TTransport.TTransportException, tx:
pass
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
itrans.close()
otrans.close()
@@ -179,7 +181,7 @@
t.setDaemon(self.daemon)
t.start()
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
# Pump the socket for clients
self.serverTransport.listen()
@@ -190,7 +192,7 @@
continue
self.clients.put(client)
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
class TForkingServer(TServer):
@@ -215,7 +217,7 @@
try:
file.close()
except IOError, e:
- logging.warning(e, exc_info=True)
+ logger.warning(e, exc_info=True)
self.serverTransport.listen()
while True:
@@ -251,7 +253,7 @@
except TTransport.TTransportException, tx:
pass
except Exception, e:
- logging.exception(e)
+ logger.exception(e)
ecode = 1
finally:
try_close(itrans)
@@ -262,7 +264,7 @@
except TTransport.TTransportException, tx:
pass
except Exception, x:
- logging.exception(x)
+ logger.exception(x)
def collect_children(self):
while self.children: