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 ...@@ -17,12 +17,6 @@ import smtplib
import logging import logging
logger = logging.getLogger('django_notify')
if not logger.handlers:
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)
class Command(BaseCommand): class Command(BaseCommand):
help = 'Sends notification emails to subscribed users taking into account the subscription interval' #@ReservedAssignment help = 'Sends notification emails to subscribed users taking into account the subscription interval' #@ReservedAssignment
option_list = BaseCommand.option_list + ( option_list = BaseCommand.option_list + (
...@@ -43,12 +37,18 @@ class Command(BaseCommand): ...@@ -43,12 +37,18 @@ class Command(BaseCommand):
subject, message, notify_settings.EMAIL_SENDER, subject, message, notify_settings.EMAIL_SENDER,
[context['user'].email], connection=connection [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) email.send(fail_silently=False)
def handle(self, *args, **options): 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: if not notify_settings.SEND_EMAILS:
print "E-mails disabled - quitting." print "E-mails disabled - quitting."
...@@ -58,12 +58,12 @@ class Command(BaseCommand): ...@@ -58,12 +58,12 @@ class Command(BaseCommand):
# Run as daemon, ie. fork the process # Run as daemon, ie. fork the process
if daemon: if daemon:
logger.info("Daemon mode enabled, forking") self.logger.info("Daemon mode enabled, forking")
try: try:
fpid = os.fork() fpid = os.fork()
if fpid > 0: if fpid > 0:
# Running as daemon now. PID is fpid # 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 = file('/tmp/daemon-example.pid', "w")
pid_file.write(str(fpid)) pid_file.write(str(fpid))
pid_file.close() pid_file.close()
...@@ -90,18 +90,18 @@ class Command(BaseCommand): ...@@ -90,18 +90,18 @@ class Command(BaseCommand):
try: try:
connection = mail.get_connection() connection = mail.get_connection()
except: except:
logger.error("Could get a mail connection") self.logger.error("Could get a mail connection")
raise raise
while True: while True:
try: try:
connection.open() connection.open()
except: except:
logger.error("Could not use e-mail connection") self.logger.error("Could not use e-mail connection")
raise raise
started_sending_at = datetime.now() 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: if last_sent:
settings = models.Settings.objects.filter( settings = models.Settings.objects.filter(
...@@ -128,7 +128,7 @@ class Command(BaseCommand): ...@@ -128,7 +128,7 @@ class Command(BaseCommand):
n.save() n.save()
except smtplib.SMTPException: except smtplib.SMTPException:
# TODO: Only quit on certain errors, retry on others. # 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 raise
connection.close() 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