Skip to content

Commit

Permalink
Merge pull request #252 from timvaillancourt/stdout_logging
Browse files Browse the repository at this point in the history
Log regular console output to stdout, only use stderr for errors. Crazy eh?
  • Loading branch information
dbmurphy authored Feb 22, 2018
2 parents 6ca1a97 + 6c524bc commit 53bede8
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions mongodb_consistent_backup/Logger.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import logging
import os
import sys

from gzip import GzipFile


class StdoutLogFilter(object):
def __init__(self, max_level):
self.max_level = max_level

def filter(self, entry):
return entry.levelno < self.max_level


class Logger:
def __init__(self, config, backup_time):
self.config = config
self.backup_name = self.config.backup.name
self.backup_time = backup_time

self.log_level = logging.INFO
self.stderr_level = logging.ERROR
self.stdout_level = logging.INFO
if self.config.verbose:
self.log_level = logging.DEBUG
self.stdout_level = logging.DEBUG

self.do_file_log = False
if self.config.log_dir is not '':
Expand All @@ -27,7 +37,20 @@ def __init__(self, config, backup_time):

def start(self):
try:
logging.basicConfig(level=self.log_level, format=self.log_format)
logging.getLogger('').setLevel(self.stdout_level)

# stdout logging: DEBUG/INFO -> WARNING
stdout = logging.StreamHandler(sys.stdout)
stdout.setLevel(self.stdout_level)
stdout.setFormatter(logging.Formatter(self.log_format))
stdout.addFilter(StdoutLogFilter(self.stderr_level))
logging.getLogger('').addHandler(stdout)

# sterrr logging: ERROR -> FATAL
stderr = logging.StreamHandler(sys.stderr)
stderr.setLevel(self.stderr_level)
stderr.setFormatter(logging.Formatter(self.log_format))
logging.getLogger('').addHandler(stderr)
except Exception, e:
print("Could not start logger: %s" % e)
raise e
Expand All @@ -38,7 +61,7 @@ def start_file_logger(self):
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.setLevel(self.stdout_level)
self.file_log.setFormatter(logging.Formatter(self.log_format))
logging.getLogger('').addHandler(self.file_log)
except OSError:
Expand Down

0 comments on commit 53bede8

Please sign in to comment.