Permalink
Browse files

added base command for logging and such

  • Loading branch information...
1 parent 8ae646c commit af02120ba515f9ecc44fb1bb7b699f29a142add1 @WoLpH committed Aug 27, 2013
Showing with 33 additions and 0 deletions.
  1. +33 −0 django_utils/management/commands/base_command.py
@@ -0,0 +1,33 @@
+import logging
+from django.core.management.base import BaseCommand
+
+DEFAULT_VERBOSITY = 2
+VERBOSITY_LOG_MAP = {
+ 0: logging.ERROR,
+ 1: logging.WARN,
+ 2: logging.INFO,
+ 3: logging.DEBUG,
+}
+
+
+class CustomBaseCommand(BaseCommand):
+
+ def __init__(self):
+ self.verbosity = DEFAULT_VERBOSITY
+ BaseCommand.__init__(self)
+
+ def handle(self, *args, **kwargs):
+ self.verbosity = int(kwargs.get('verbosity', DEFAULT_VERBOSITY))
+ self.log = self.create_logger()
+ self.debug = self.log.debug
+ self.info = self.log.info
+ self.warn = self.log.warn
+ self.error = self.log.error
+
+ def create_logger(self):
+ name = self.__class__.__module__.split('.')[-1]
+
+ logger_name = 'management.commands.%s' % name
+ logger = logging.getLogger(logger_name)
+ logger.setLevel(VERBOSITY_LOG_MAP[self.verbosity])
+ return logger

0 comments on commit af02120

Please sign in to comment.