Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions mongodb_consistent_backup/Logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ def __init__(self, config, backup_time):

self.do_file_log = False
if self.config.log_dir is not '':
if os.path.isdir(self.config.log_dir):
self.do_file_log = True
else:
print("ERROR: Log directory: %s does not exist! Skipping file-based logging" % self.config.log_dir)
self.do_file_log = True
if not os.path.isdir(self.config.log_dir):
os.mkdir(self.config.log_dir)

self.log_format = '[%(asctime)s] [%(levelname)s] [%(processName)s] [%(module)s:%(funcName)s:%(lineno)d] %(message)s'
self.file_log = None
Expand All @@ -35,15 +34,12 @@ def start(self):
def start_file_logger(self):
if self.do_file_log:
try:
if os.path.isdir(self.config.log_dir):
os.mkdir(self.config.log_dir)
self.current_log_file = os.path.join(self.config.log_dir, "backup.%s.log" % self.backup_name)
self.backup_log_file = os.path.join(self.config.log_dir, "backup.%s.%s.log" % (self.backup_name, self.backup_time))
self.file_log = logging.FileHandler(self.backup_log_file)
self.file_log.setLevel(self.log_level)
self.file_log.setFormatter(logging.Formatter(self.log_format))
logging.getLogger('').addHandler(self.file_log)
self.update_symlink()
except OSError, e:
logging.warning("Could not start file log handler, writing to stdout only")
pass
Expand All @@ -52,18 +48,21 @@ def close(self):
if self.file_log:
self.file_log.close()

def compress(self):
def compress(self, current=False):
gz_log = None
try:
if not os.path.isfile(self.last_log) or self.last_log == self.backup_log_file:
return
logging.info("Compressing previous log file")
gz_file = "%s.gz" % self.last_log
compress_file = self.backup_log_file
if not current:
compress_file = self.last_log
if not os.path.isfile(self.last_log) or self.last_log == self.backup_log_file:
return
logging.info("Compressing log file: %s" % compress_file)
gz_file = "%s.gz" % compress_file
gz_log = GzipFile(gz_file, "w+")
with open(self.last_log) as f:
with open(compress_file) as f:
for line in f:
gz_log.write(line)
os.remove(self.last_log)
os.remove(compress_file)
finally:
if gz_log:
gz_log.close()
Expand Down
4 changes: 3 additions & 1 deletion mongodb_consistent_backup/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, prog_name="mongodb-consistent-backup"):
self.setup_logger()
self.setup_signal_handlers()
self.get_lock()
self.logger.start_file_logger()
self.logger.update_symlink()
self.init()
self.set_backup_dirs()
self.get_db_conn()
Expand All @@ -74,6 +74,7 @@ def setup_logger(self):
try:
self.logger = Logger(self.config, self.backup_time)
self.logger.start()
self.logger.start_file_logger()
except Exception, e:
self.exception("Could not start logger: %s" % e, e)

Expand Down Expand Up @@ -117,6 +118,7 @@ def get_lock(self):
self.lock = Lock(self.config.lock_file)
except Exception:
logging.fatal("Could not acquire lock: '%s'! Is another %s process running? Exiting" % (self.config.lock_file, self.program_name))
self.logger.compress(True)
sys.exit(1)

def release_lock(self):
Expand Down