Use logging module to make logging threadsafe (avoid splitting log lines)

This commit is contained in:
Stuart Gathman
2005-10-10 23:50:20 +00:00
parent d34efa39bb
commit 78ea2e2263
+22 -15
View File
@@ -1,6 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
# A simple milter that has grown quite a bit. # A simple milter that has grown quite a bit.
# $Log$ # $Log$
# Revision 1.27 2005/10/10 20:15:33 customdesigned
# Configure SPF policy via sendmail access file.
#
# Revision 1.26 2005/10/07 03:23:40 customdesigned # Revision 1.26 2005/10/07 03:23:40 customdesigned
# Banned users option. Experimental feature to supply Sender when # Banned users option. Experimental feature to supply Sender when
# missing and MFROM domain doesn't match From. Log cipher bits for # missing and MFROM domain doesn't match From. Log cipher bits for
@@ -295,8 +298,7 @@ try: import spf
except: spf = None except: spf = None
ip4re = re.compile(r'^[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*$') ip4re = re.compile(r'^[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*$')
#import syslog import logging
#syslog.openlog('milter')
# Thanks to Chris Liechti for config parsing suggestions # Thanks to Chris Liechti for config parsing suggestions
@@ -347,6 +349,14 @@ access_file = None
time_format = '%Y%b%d %H:%M:%S %Z' time_format = '%Y%b%d %H:%M:%S %Z'
timeout = 600 timeout = 600
cbv_cache = {} cbv_cache = {}
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format='%(asctime)s %(message)s',
datefmt='%Y%b%d %H:%M:%S'
)
milter_log = logging.getLogger('milter')
try: try:
too_old = time.time() - 7*24*60*60 # 7 days too_old = time.time() - 7*24*60*60 # 7 days
for ln in open('send_dsn.log'): for ln in open('send_dsn.log'):
@@ -474,7 +484,7 @@ def read_config(list):
for sa in cp.getlist('wiretap','smart_alias'): for sa in cp.getlist('wiretap','smart_alias'):
sm = cp.getlist('wiretap',sa) sm = cp.getlist('wiretap',sa)
if len(sm) < 2: if len(sm) < 2:
print 'malformed smart alias:',sa milter_log.warning('malformed smart alias: %s',sa)
continue continue
if len(sm) == 2: sm.append(sa) if len(sm) == 2: sm.append(sa)
key = (sm[0],sm[1]) key = (sm[0],sm[1])
@@ -652,9 +662,7 @@ class bmsMilter(Milter.Milter):
and smart alias redirection.""" and smart alias redirection."""
def log(self,*msg): def log(self,*msg):
print "%s [%d]" % (time.strftime('%Y%b%d %H:%M:%S'),self.id), milter_log.info('[%d] %s',self.id,' '.join([str(m) for m in msg]))
for i in msg: print i,
print
def __init__(self): def __init__(self):
self.tempname = None self.tempname = None
@@ -1313,7 +1321,7 @@ class bmsMilter(Milter.Milter):
modified = True modified = True
except Exception,x: except Exception,x:
self.log("check_spam:",x) self.log("check_spam:",x)
traceback.print_exc() milter_log.error("check_spam: %s",x,exc_info=True)
# screen if no recipients are dspam_users # screen if no recipients are dspam_users
if not modified and dspam_screener and not self.internal_connection \ if not modified and dspam_screener and not self.internal_connection \
and self.dspam: and self.dspam:
@@ -1373,7 +1381,7 @@ class bmsMilter(Milter.Milter):
if not exc_value.strerror: if not exc_value.strerror:
exc_value.strerror = exc_value.args[0] exc_value.strerror = exc_value.args[0]
if exc_value.strerror == 'Lock failed': if exc_value.strerror == 'Lock failed':
self.log("LOCK: BUSY") # log filename milter_log.warn("LOCK: BUSY") # log filename
self.setreply('450','4.2.0', self.setreply('450','4.2.0',
'Too busy discarding spam. Please try again later.') 'Too busy discarding spam. Please try again later.')
return Milter.TEMPFAIL return Milter.TEMPFAIL
@@ -1381,7 +1389,7 @@ class bmsMilter(Milter.Milter):
os.rename(self.tempname,fname) os.rename(self.tempname,fname)
self.tempname = None self.tempname = None
if exc_type == email.Errors.BoundaryError: if exc_type == email.Errors.BoundaryError:
self.log("MALFORMED: %s" % fname) # log filename milter_log.warn("MALFORMED: %s",fname) # log filename
if self.internal_connection: if self.internal_connection:
# accept anyway for now # accept anyway for now
return Milter.ACCEPT return Milter.ACCEPT
@@ -1389,12 +1397,12 @@ class bmsMilter(Milter.Milter):
'Boundary error in your message, are you a spammer?') 'Boundary error in your message, are you a spammer?')
return Milter.REJECT return Milter.REJECT
if exc_type == email.Errors.HeaderParseError: if exc_type == email.Errors.HeaderParseError:
self.log("MALFORMED: %s" % fname) # log filename milter_log.warn("MALFORMED: %s",fname) # log filename
self.setreply('554','5.7.7', self.setreply('554','5.7.7',
'Header parse error in your message, are you a spammer?') 'Header parse error in your message, are you a spammer?')
return Milter.REJECT return Milter.REJECT
milter_log.error("FAIL: %s",fname) # log filename
# let default exception handler print traceback and return 451 code # let default exception handler print traceback and return 451 code
self.log("FAIL: %s" % fname) # log filename
raise raise
if rc == Milter.REJECT: return rc; if rc == Milter.REJECT: return rc;
if rc == Milter.DISCARD: return rc; if rc == Milter.DISCARD: return rc;
@@ -1499,12 +1507,11 @@ class bmsMilter(Milter.Milter):
return Milter.CONTINUE return Milter.CONTINUE
def close(self): def close(self):
sys.stdout.flush() # make log messages visible
if self.tempname: if self.tempname:
os.remove(self.tempname) # remove in case session aborted os.remove(self.tempname) # remove in case session aborted
if self.fp: if self.fp:
self.fp.close() self.fp.close()
sys.stdout.flush()
return Milter.CONTINUE return Milter.CONTINUE
def abort(self): def abort(self):
@@ -1519,10 +1526,10 @@ def main():
if srs or len(discard_users) > 0 or smart_alias or dspam_userdir: if srs or len(discard_users) > 0 or smart_alias or dspam_userdir:
flags = flags + Milter.DELRCPT flags = flags + Milter.DELRCPT
Milter.set_flags(flags) Milter.set_flags(flags)
print "%s bms milter startup" % time.strftime('%Y%b%d %H:%M:%S') milter_log.info("bms milter startup")
sys.stdout.flush() sys.stdout.flush()
Milter.runmilter("pythonfilter",socketname,timeout) Milter.runmilter("pythonfilter",socketname,timeout)
print "%s bms milter shutdown" % time.strftime('%Y%b%d %H:%M:%S') milter_log.info("bms milter shutdown")
if __name__ == "__main__": if __name__ == "__main__":
read_config(["/etc/mail/pymilter.cfg","milter.cfg"]) read_config(["/etc/mail/pymilter.cfg","milter.cfg"])