Ken Kinder's personal website

Making httplib log debug information

As opposed to having it print to stdout. Here’s how…

--- httplib.py.orig	2008-12-16 19:14:07.000000000 -0600
+++ httplib.py	2008-12-16 19:26:20.000000000 -0600
@@ -69,6 +69,7 @@
 import errno
 import mimetools
 import socket
+import logging
 from urlparse import urlsplit
 
 try:
@@ -342,7 +343,7 @@
         # Initialize with Simple-Response defaults
         line = self.fp.readline()
         if self.debuglevel > 0:
-            print "reply:", repr(line)
+            logging.info("reply: %r" % (line,))
         if not line:
             # Presumably, the server closed the connection before
             # sending a valid response.
@@ -391,7 +392,7 @@
                 if not skip:
                     break
                 if self.debuglevel > 0:
-                    print "header:", skip
+                    logging.info('header: %r' % (skip,))
 
         self.status = status
         self.reason = reason.strip()
@@ -414,7 +415,7 @@
         self.msg = HTTPMessage(self.fp, 0)
         if self.debuglevel > 0:
             for hdr in self.msg.headers:
-                print "header:", hdr,
+                logging.info('header: %r' % (hdr,))
 
         # don't let the msg keep an fp
         self.msg.fp = None
@@ -665,11 +666,11 @@
             try:
                 self.sock = socket.socket(af, socktype, proto)
                 if self.debuglevel > 0:
-                    print "connect: (%s, %s)" % (self.host, self.port)
+                    logging.info("connect: (%s, %s)" % (self.host, self.port))
                 self.sock.connect(sa)
             except socket.error, msg:
                 if self.debuglevel > 0:
-                    print 'connect fail:', (self.host, self.port)
+                    logging.info('connect fail:', (self.host, self.port))
                 if self.sock:
                     self.sock.close()
                 self.sock = None
@@ -702,7 +703,7 @@
         # NOTE: we DO propagate the error, though, because we cannot simply
         #       ignore the error... the caller will know if they can retry.
         if self.debuglevel > 0:
-            print "send:", repr(str)
+            logging.info("send: %s" % (str,))
         try:
             self.sock.sendall(str)
         except socket.error, v:

httplib-logging-patch

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">