Commits

Colin Copeland  committed b3e8d95

add logging configuration

  • Participants
  • Parent commits c0c136c

Comments (0)

Files changed (2)

     'MySQL',
 )
 
-
+logger = logging.getLogger('dbbu')
 DATABASES_TO_IGNORE = set(['template0'])
-logging.basicConfig(stream=sys.stdout,
-                    level=logging.DEBUG)
 
 
 class Backup(object):
         self.databases = set(databases)
         if not os.path.exists(self.dest):
             os.makedirs(self.dest)
+        self.logger = logging.getLogger('dbbu.%s' % self.host.replace('.', '-'))
 
     def execute(self, cmd, comm=True, **kwargs):
         if 'stderr' not in kwargs:
             kwargs['stderr'] = subprocess.PIPE
-        logging.debug(cmd)
+        self.logger.debug(cmd)
         p = subprocess.Popen(cmd, shell=True, **kwargs)
         if comm:
             out, err = p.communicate()
             if err:
-                logging.error(err)
+                self.logger.error(err)
             p.out = out
             p.err = err
         return p
     
     def chmod(self, path):
         os.chmod(path, self.fmod)
+    
+    
 
 
 class PostgreSQL(Backup):
         if self.databases:
             databases = self.databases
         databases -= DATABASES_TO_IGNORE
-        logging.info('databases: %s', databases)
+        self.logger.info('databases: %s', databases)
         for database in databases:
-            logging.info('backing up %s' % database)
+            self.logger.info('backing up %s' % database)
             self.backup_postgres_database(database)
 
     def get_postgres_databases(self):

File scripts/dbbu-run.py

 import sys
 import time
 import string
+import logging
+import logging.handlers
 import ConfigParser
 from optparse import OptionParser
 import dbbu
 [myserver.com]"""
 parser = OptionParser(usage=usage)
 
+ALLOWED_LEVELS = ('NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
+logger = logging.getLogger('dbbu')
+formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)-8s %(message)s',
+                              datefmt='%a, %d %b %Y %H:%M:%S')
+
+def setup_logging(cfg):
+    log_level = cfg.get('default', 'log_level')
+    if log_level not in ALLOWED_LEVELS:
+        sys.stderr.write('Invalid log level\n\n')
+        log_level = logging.NOTSET
+    else:
+        log_level = getattr(logging, log_level)
+    logger.setLevel(log_level)
+    
+    if cfg.has_option('default', 'log_file'):
+        log_file = cfg.get('default', 'log_file')
+        handler = logging.handlers.RotatingFileHandler(log_file,
+                                                       backupCount=5)
+        handler.setFormatter(formatter)
+        logger.addHandler(handler)
+    else:
+        handler = logging.StreamHandler()
+        handler.setFormatter(formatter)
+        logger.addHandler(handler)
+
 def main():
     (options, cfg_path) = parser.parse_args()
     if not cfg_path:
         sys.stderr.write('Configuration file required!\n\n')
         parser.print_usage()
         sys.exit(-1)
-    defaults = {'fmod': '0600', 'compression': 'gzip', 'dest': os.getcwd()}
+    defaults = {'fmod': '0600',
+                'compression': 'gzip',
+                'dest': os.getcwd(),
+                'log_level': 'INFO'}
     cfg = ConfigParser.RawConfigParser(defaults)
     cfg.read(cfg_path)
+    setup_logging(cfg)
     shared = {
         'dest': cfg.get('default', 'dest'),
         'compression': cfg.get('default', 'compression'),