home *** CD-ROM | disk | FTP | other *** search
- #
- # Logger.py
- # JunkMatcher
- #
- # Created by Benjamin Han on 2/1/05.
- # Copyright (c) 2005 Benjamin Han. All rights reserved.
- #
-
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License
- # as published by the Free Software Foundation; either version 2
- # of the License, or (at your option) any later version.
-
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
-
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
- #!/usr/bin/env python
-
- import logging, cPickle, os, time
-
-
- class Logger:
- """JunkMatcher log is in effect an index to EmailDB."""
- def __init__ (self, logFN):
- self.logFN = logFN
-
- self.logger = logging.getLogger('JunkMatcher')
- self.logger.setLevel(logging.INFO)
-
- self.hdlr = logging.FileHandler(logFN)
- self.hdlr.setFormatter(logging.Formatter(''))
- self.logger.addHandler(self.hdlr)
-
- def info (self, k, msg, matchResult):
- if msg.m:
- s = cPickle.dumps((msg.receivedDate, k, msg.sender,
- msg.subject, matchResult.verdict, matchResult), cPickle.HIGHEST_PROTOCOL)
- else:
- s = cPickle.dumps((msg.receivedDate, k, u'', u'',
- matchResult.verdict, matchResult), cPickle.HIGHEST_PROTOCOL)
-
- self.logger.info('%d\n%s' % (len(s), s))
-
- def recycle (self):
- self.hdlr.close()
- self.logger.removeHandler(self.hdlr)
-
- try:
- os.remove(self.logFN)
- except:
- pass
-
- self.hdlr = logging.FileHandler(self.logFN)
- self.hdlr.setFormatter(logging.Formatter(''))
- self.logger.addHandler(self.hdlr)
-