Permalink
Browse files

[#1043] Extracted logging setup code to its own file for reuse.

  • Loading branch information...
arowla committed Mar 3, 2012
1 parent a67e8b7 commit e6cbb37129451371e5b59a6b750319dacea2ba3d
Showing with 37 additions and 30 deletions.
  1. +4 −30 dcdata/management/base/importer.py
  2. +33 −0 dcdata/utils/log.py
@@ -1,14 +1,13 @@
import os
import os.path
import fnmatch
-import logging
-import logging.handlers
import time
import datetime
-from settings import LOGGING_EMAIL, LOGGING_DIRECTORY, TMP_DIRECTORY
+from dcdata.utils.logging import set_up_logger
from django.core.management.base import BaseCommand, CommandError
-from optparse import make_option
+from optparse import make_option
+from settings import LOGGING_DIRECTORY, TMP_DIRECTORY
class BaseImporter(BaseCommand):
@@ -38,34 +37,9 @@ def __init__(self):
super(BaseImporter, self).__init__()
self.class_name = self.__class__.__name__
self.log_path = os.path.join(LOGGING_DIRECTORY, self.__module__.split('.')[-1])
- self.set_up_logger()
+ self.log = set_up_logger(self.class_name, self.log_path, self.email_subject)
self.pid_file_path = os.path.join(TMP_DIRECTORY, self.__module__.split('.')[-1])
- def set_up_logger(self):
- # create logger
- self.log = logging.getLogger(self.class_name)
- self.log.setLevel(logging.DEBUG)
- # create console handler and set level to debug
- ch = logging.FileHandler(self.log_path)
- ch.setLevel(logging.DEBUG)
-
- # create email handler and set level to warn
- eh = logging.handlers.SMTPHandler(
- (LOGGING_EMAIL['host'], LOGGING_EMAIL['port']), # host
- LOGGING_EMAIL['username'], # from address
- ['arowland@sunlightfoundation.com'], # to addresses
- self.email_subject,
- (LOGGING_EMAIL['username'], LOGGING_EMAIL['password']) # credentials tuple
- )
- eh.setLevel(logging.WARN)
-
- # create formatter
- formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
- ch.setFormatter(formatter)
- eh.setFormatter(formatter)
- self.log.addHandler(ch)
- self.log.addHandler(eh)
-
def handle(self, *args, **options):
"""
View
@@ -0,0 +1,33 @@
+import logging
+import logging.handlers
+import os.path
+
+from settings import LOGGING_EMAIL
+
+def set_up_logger(importer_name, log_path, email_subject):
+ # create logger
+ log = logging.getLogger(importer_name)
+ log.setLevel(logging.DEBUG)
+ # create console handler and set level to debug
+ ch = logging.FileHandler(os.path.join(log_path, importer_name + '.log'))
+ ch.setLevel(logging.DEBUG)
+
+ # create email handler and set level to warn
+ eh = logging.handlers.SMTPHandler(
+ (LOGGING_EMAIL['host'], LOGGING_EMAIL['port']), # host
+ LOGGING_EMAIL['username'], # from address
+ ['arowland@sunlightfoundation.com'], # to addresses
+ email_subject,
+ (LOGGING_EMAIL['username'], LOGGING_EMAIL['password']) # credentials tuple
+ )
+ eh.setLevel(logging.WARN)
+
+ # create formatter
+ formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
+ ch.setFormatter(formatter)
+ eh.setFormatter(formatter)
+ log.addHandler(ch)
+ log.addHandler(eh)
+
+ return log
+

0 comments on commit e6cbb37

Please sign in to comment.