Permalink
Browse files

Logging is now significnatly more configurable.

  • Loading branch information...
bcoe committed Dec 1, 2011
1 parent 935eab4 commit 2314293abf7e822be7efee5061fc1f806e7aeb54
View
@@ -1,4 +1,5 @@
-import asyncore
+import secure_smtpd
+import asyncore, logging
from secure_smtpd import SMTPServer, FakeCredentialValidator
class SSLSMTPServer(SMTPServer):
@@ -18,10 +19,11 @@ def start(self):
ssl=True,
certfile='examples/server.crt',
keyfile='examples/server.key',
- credential_validator=FakeCredentialValidator(),
- debug=True
+ credential_validator=FakeCredentialValidator()
)
asyncore.loop()
-
+
+logger = logging.getLogger( secure_smtpd.LOG_NAME )
+logger.setLevel(logging.INFO)
server = SSLSMTPServer()
-server.start()
+server.start()
View
@@ -1,20 +1,4 @@
-import logging
-from logging.handlers import RotatingFileHandler
+import secure_smtpd.config
+from secure_smtpd.config import LOG_NAME
from smtp_server import SMTPServer
from fake_credential_validator import FakeCredentialValidator
-
-def _create_logger():
- log_name = '/var/log/secure_smtpd.log'
- logger = logging.getLogger( log_name )
- logger.setLevel(logging.DEBUG)
- handler = RotatingFileHandler(
- log_name,
- maxBytes=10485760,
- backupCount=3
- )
- formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
- handler.setFormatter(formatter)
- logger.addHandler(handler)
- return logger
-
-logger = _create_logger()
@@ -0,0 +1,3 @@
+import log
+
+LOG_NAME = 'secure-smtp'
View
@@ -0,0 +1,26 @@
+import logging
+from logging.handlers import RotatingFileHandler
+
+_logger_initialized = False
+
+def _initialize_logger():
+ global _logger_initialized
+
+ log_name = 'secure-smtp'
+ logger = logging.getLogger( log_name )
+
+ handler = RotatingFileHandler(
+ '/var/log/%s.log' % log_name,
+ maxBytes=10485760,
+ backupCount=3
+ )
+ logger.addHandler(handler)
+
+ formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
+ handler.setFormatter(formatter)
+ logger.setLevel(logging.WARN)
+
+ _logger_initialized = True
+
+if not _logger_initialized:
+ _initialize_logger()
@@ -1,13 +1,15 @@
import secure_smtpd
+import logging
# Implment this interface with an actual
# methodlogy for validating credentials, e.g.,
-# lookup credentials for a usaer in Redis.
+# lookup credentials for a user in Redis.
class FakeCredentialValidator(object):
def validate(self, username, password):
- secure_smtpd.logger.warn('FakeCredentialValidator: you should replace this with an actual implementation of a credential validator.')
+ logger = logging.getLogger( secure_smtpd.LOG_NAME )
+ logger.warn('FakeCredentialValidator: you should replace this with an actual implementation of a credential validator.')
if username == 'bcoe' and password == 'foobar':
return True
@@ -1,19 +1,21 @@
-import smtpd, base64, secure_smtpd, asynchat
+import secure_smtpd
+import smtpd, base64, secure_smtpd, asynchat, logging
+
from asyncore import ExitNow
from smtpd import NEWLINE, EMPTYSTRING
class SMTPChannel(smtpd.SMTPChannel):
- def __init__(self, smtp_server, newsocket, fromaddr, require_authentication=False, credential_validator=None, debug=False, map=None):
+ def __init__(self, smtp_server, newsocket, fromaddr, require_authentication=False, credential_validator=None, map=None):
smtpd.SMTPChannel.__init__(self, smtp_server, newsocket, fromaddr)
asynchat.async_chat.__init__(self, newsocket, map=map)
self.require_authentication = require_authentication
self.authenticating = False
self.authenticated = False
self.username = None
self.password = None
- self.debug = debug
self.credential_validator = credential_validator
+ self.logger = logging.getLogger( secure_smtpd.LOG_NAME )
def smtp_QUIT(self, arg):
self.push('221 Bye')
@@ -67,7 +69,7 @@ def found_terminator(self):
line = EMPTYSTRING.join(self.__line)
if self.debug:
- secure_smtpd.logger.info('found_terminator(): data: %s' % repr(line))
+ self.logger.info('found_terminator(): data: %s' % repr(line))
self.__line = []
if self.__state == self.COMMAND:
@@ -1,5 +1,6 @@
import secure_smtpd
-import ssl, smtpd, asyncore, socket
+import ssl, smtpd, asyncore, socket, logging
+
from smtp_channel import SMTPChannel
from asyncore import ExitNow
from process_pool import ProcessPool
@@ -8,9 +9,9 @@
class SMTPServer(smtpd.SMTPServer):
- def __init__(self, localaddr, remoteaddr, ssl=False, certfile=None, keyfile=None, ssl_version=ssl.PROTOCOL_SSLv23, require_authentication=False, credential_validator=None, debug=False, maximum_execution_time=30, process_count=5):
+ def __init__(self, localaddr, remoteaddr, ssl=False, certfile=None, keyfile=None, ssl_version=ssl.PROTOCOL_SSLv23, require_authentication=False, credential_validator=None, maximum_execution_time=30, process_count=5):
smtpd.SMTPServer.__init__(self, localaddr, remoteaddr)
- self.debug = debug
+ self.logger = logging.getLogger( secure_smtpd.LOG_NAME )
self.certfile = certfile
self.keyfile = keyfile
self.ssl_version = ssl_version
@@ -21,13 +22,13 @@ def __init__(self, localaddr, remoteaddr, ssl=False, certfile=None, keyfile=None
self.maximum_execution_time = maximum_execution_time
self.process_count = process_count
self.process_pool = None
+
def handle_accept(self):
if not self.process_pool:
self.process_pool = ProcessPool(self._accept_subprocess, process_count=self.process_count)
- if self.debug:
- secure_smtpd.logger.info('handle_accept(): called.')
+ self.logger.info('handle_accept(): called.')
self.process_pool.handle_accept()
@@ -43,8 +44,7 @@ def _accept_subprocess(self, queue):
pair = self.accept()
map = {}
- if self.debug:
- secure_smtpd.logger.info('_accept_subprocess(): smtp connection accepted within subprocess.')
+ self.logger.info('_accept_subprocess(): smtp connection accepted within subprocess.')
if pair is not None:
@@ -65,17 +65,15 @@ def _accept_subprocess(self, queue):
fromaddr,
require_authentication=self.require_authentication,
credential_validator=self.credential_validator,
- debug=self.debug,
map=map
)
- if self.debug:
- secure_smtpd.logger.info('_accept_subprocess(): starting asyncore within subprocess.')
+ self.logger.info('_accept_subprocess(): starting asyncore within subprocess.')
asyncore.loop(map=map)
- secure_smtpd.logger.error('_accept_subprocess(): asyncore loop exited.')
+
+ self.logger.error('_accept_subprocess(): asyncore loop exited.')
except (ExitNow, SSLError):
newsocket.shutdown(socket.SHUT_RDWR)
newsocket.close()
- if self.debug:
- secure_smtpd.logger.info('_accept_subprocess(): smtp channel terminated asyncore.')
+ self.logger.info('_accept_subprocess(): smtp channel terminated asyncore.')
View
@@ -4,7 +4,7 @@
setup(
name="secure-smtpd",
- version="1.1.1",
+ version="1.1.2",
description="Adds support for SSL, AUTH, and other goodies, to Petri Lehtinen's SMTPD library.",
author="Benjamin Coe",
author_email="bencoe@gmail.com",

0 comments on commit 2314293

Please sign in to comment.