Commit 6641ed1d by benjaoming

use self.stdout in django_notify management script logging (see django docs:…

use self.stdout in django_notify management script logging (see django docs: https://docs.djangoproject.com/en/dev/howto/custom-management-commands/)
parent df27e510
......@@ -17,12 +17,6 @@ import smtplib
import logging
logger = logging.getLogger('django_notify')
if not logger.handlers:
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)
class Command(BaseCommand):
help = 'Sends notification emails to subscribed users taking into account the subscription interval' #@ReservedAssignment
option_list = BaseCommand.option_list + (
......@@ -43,12 +37,18 @@ class Command(BaseCommand):
subject, message, notify_settings.EMAIL_SENDER,
[context['user'].email], connection=connection
)
logger.info("Sending to: %s" % context['user'].email)
self.logger.info("Sending to: %s" % context['user'].email)
email.send(fail_silently=False)
def handle(self, *args, **options):
logger.info("Starting django_notify e-mail dispatcher")
self.logger = logging.getLogger('django_notify')
if not self.logger.handlers:
self.logger.addHandler(logging.StreamHandler(stream=self.stdout))
self.logger.setLevel(logging.INFO)
self.logger.info("Starting django_notify e-mail dispatcher")
if not notify_settings.SEND_EMAILS:
print "E-mails disabled - quitting."
......@@ -58,12 +58,12 @@ class Command(BaseCommand):
# Run as daemon, ie. fork the process
if daemon:
logger.info("Daemon mode enabled, forking")
self.logger.info("Daemon mode enabled, forking")
try:
fpid = os.fork()
if fpid > 0:
# Running as daemon now. PID is fpid
logger.info("PID: %s" % str(fpid))
self.logger.info("PID: %s" % str(fpid))
pid_file = file('/tmp/daemon-example.pid', "w")
pid_file.write(str(fpid))
pid_file.close()
......@@ -90,18 +90,18 @@ class Command(BaseCommand):
try:
connection = mail.get_connection()
except:
logger.error("Could get a mail connection")
self.logger.error("Could get a mail connection")
raise
while True:
try:
connection.open()
except:
logger.error("Could not use e-mail connection")
self.logger.error("Could not use e-mail connection")
raise
started_sending_at = datetime.now()
logger.info("Starting send loop at %s" % str(started_sending_at))
self.logger.info("Starting send loop at %s" % str(started_sending_at))
if last_sent:
settings = models.Settings.objects.filter(
......@@ -128,7 +128,7 @@ class Command(BaseCommand):
n.save()
except smtplib.SMTPException:
# TODO: Only quit on certain errors, retry on others.
logger.error("You have an error with your SMTP server connection, quitting.")
self.logger.error("You have an error with your SMTP server connection, quitting.")
raise
connection.close()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment