home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / JunkMatcher 1.5.5.dmg / JunkMatcher.app / Contents / Resources / Engine / Logger.py < prev    next >
Encoding:
Python Source  |  2005-06-01  |  2.0 KB  |  62 lines

  1. #
  2. #  Logger.py
  3. #  JunkMatcher
  4. #
  5. #  Created by Benjamin Han on 2/1/05.
  6. #  Copyright (c) 2005 Benjamin Han. All rights reserved.
  7. #
  8.  
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13.  
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18.  
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  
  23. #!/usr/bin/env python
  24.  
  25. import logging, cPickle, os, time
  26.  
  27.  
  28. class Logger:
  29.     """JunkMatcher log is in effect an index to EmailDB."""
  30.     def __init__ (self, logFN):
  31.         self.logFN = logFN            
  32.         
  33.         self.logger = logging.getLogger('JunkMatcher')
  34.         self.logger.setLevel(logging.INFO)
  35.  
  36.         self.hdlr = logging.FileHandler(logFN)
  37.         self.hdlr.setFormatter(logging.Formatter(''))
  38.         self.logger.addHandler(self.hdlr)
  39.  
  40.     def info (self, k, msg, matchResult):
  41.         if msg.m:
  42.             s = cPickle.dumps((msg.receivedDate, k, msg.sender,
  43.                                msg.subject, matchResult.verdict, matchResult), cPickle.HIGHEST_PROTOCOL)
  44.         else:
  45.             s = cPickle.dumps((msg.receivedDate, k, u'', u'',
  46.                                matchResult.verdict, matchResult), cPickle.HIGHEST_PROTOCOL)
  47.             
  48.         self.logger.info('%d\n%s' % (len(s), s))
  49.  
  50.     def recycle (self):
  51.         self.hdlr.close()
  52.         self.logger.removeHandler(self.hdlr)
  53.         
  54.         try:
  55.             os.remove(self.logFN)
  56.         except:
  57.             pass
  58.  
  59.         self.hdlr = logging.FileHandler(self.logFN)
  60.         self.hdlr.setFormatter(logging.Formatter(''))
  61.         self.logger.addHandler(self.hdlr)
  62.