[PATCH] Limit msg body length in debug output

Sebastian Spaeth Sebastian at SSpaeth.de
Wed May 4 17:00:01 BST 2011


We were outputting full message bodies to the debug log (often stderr),
and then again (as they go over the imaplib2 wire, imaplib logs
everything too). Not only is quite a privacy issue when sending in debug
logs but it can also freeze a console for quite some time. Plus it
bloats debug logs A LOT.

Only output the first and last 100 bytes of each message body to the
debug log (we still get the full body from imaplib2 logging). This
limits privacy issues when handing the log to someone else, but usually
still contains all the interesting bits that we want to see in a log.

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
This would also address a debian bug report
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=610685

 offlineimap/folder/IMAP.py |   23 ++++++++++++++++-------
 1 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py
index f2d0dda..52dc15e 100644
--- a/offlineimap/folder/IMAP.py
+++ b/offlineimap/folder/IMAP.py
@@ -213,8 +213,13 @@ class IMAPFolder(BaseFolder):
         try:
             imapobj.select(self.getfullname(), readonly = 1)
             initialresult = imapobj.uid('fetch', '%d' % uid, '(BODY.PEEK[])')
-            self.ui.debug('imap', 'Returned object from fetching %d: %s' % \
-                     (uid, str(initialresult)))
+            if len(initialresult)>200:
+                dbg_output = "%s...%s" % (str(initialresult)[:100],
+                                         str(initialresult)[-100:])
+            else:
+                dbg_output = initialresult
+            self.ui.debug('imap', 'Returned object from fetching %d: %s...%s' % \
+                     (uid, dbg_output))
             return initialresult[1][0][1].replace("\r\n", "\n")
                 
         finally:
@@ -415,21 +420,25 @@ class IMAPFolder(BaseFolder):
 
             # get the date of the message file, so we can pass it to the server.
             date = self.getmessageinternaldate(content, rtime)
-            self.ui.debug('imap', 'savemessage: using date %s' % date)
     
             content = re.sub("(?<!\r)\n", "\r\n", content)
     
             if not use_uidplus:
                 # insert a random unique header that we can fetch later
                 (headername, headervalue) = self.generate_randomheader(content)
-                self.ui.debug('imap', 'savemessage: new headers are: %s: %s' % \
+                self.ui.debug('imap', 'savemessage: new header is: %s: %s' % \
                              (headername, headervalue))
                 content = self.savemessage_addheader(content, headername,
                                                      headervalue)    
-            self.ui.debug('imap', 'savemessage: content is: ' + repr(content))
+            if len(content)>200:
+                dbg_output = "%s...%s" % (content[:100],
+                                         content[-100:])
+            else:
+                dbg_output = content
+
+            self.ui.debug('imap', 'savemessage: date: %s, content: %s'\
+                              %(date, dbg_output))
 
-            # TODO: - append could raise a ValueError if the date is not in
-            #         valid format...?
             (typ,dat) = imapobj.append(self.getfullname(),
                                        imaputil.flagsmaildir2imap(flags),
                                        date, content)
-- 
1.7.4.1





More information about the OfflineIMAP-project mailing list