Skip to content

Commit

Permalink
Added logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Dufresne committed Apr 2, 2012
1 parent 732b64a commit 11a9032
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions fakesmtpd
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@

import argparse
import asyncore
import errno
import logging
import os
import smtpd
import tempfile
import errno
import smtpd


class FakeSMTPServer(smtpd.SMTPServer):
def __init__(self, localaddr, path):
smtpd.SMTPServer.__init__(self, localaddr, None)
self.path = path
self.recipients = {}
self.path = path

self.logger = logging.getLogger('fakesmtpd')
self.logger.info("SMTP server started")
self.logger.info("Mail path %s", path)


def process_message(self, peer, mailfrom, rcpttos, data):
self.logger.info("Incoming mail from %s", mailfrom)
for recipient in rcpttos:
self.logger.info("Logging mail for %s", recipient)
count = self.recipients.get(recipient, 0) + 1
self.recipients[recipient] = count
filename = "{}.{:d}.mail".format(recipient, count)
Expand All @@ -32,15 +38,22 @@ def fakesmtpd_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-H', '--host', default='localhost')
parser.add_argument('-p', '--port', default=25)
path = os.path.join(tempfile.gettempdir(), 'fakesmtpd')
path = os.path.join(tempfile.gettempdir(),
'fakesmtpd{:d}'.format(os.getpid()))
parser.add_argument('--path', default=path)
parser.add_argument('--log')
return parser


def main():
parser = fakesmtpd_parser()
args = parser.parse_args()

logging.basicConfig(filename=args.log,
format='%(asctime)s:%(levelname)s:%(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.DEBUG)

try:
os.mkdir(args.path)
except OSError as e:
Expand Down
2 changes: 1 addition & 1 deletion fakesmtpd.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ After=syslog.target network.target
Conflicts=postfix.service exim.service sendmail.service

[Service]
ExecStart=/usr/bin/fakesmtpd
ExecStart=/usr/bin/fakesmtpd --log=/var/log/fakesmtpd.log

[Install]
WantedBy=multi-user.target

0 comments on commit 11a9032

Please sign in to comment.