diff --git a/bin/backup b/bin/backup index 21e9212..8b76238 100644 --- a/bin/backup +++ b/bin/backup @@ -13,7 +13,6 @@ Command-Line Arguments -r, --remote-host address/alias of remote machine to use -s, --run-post-sync run sync syscall after backup -u, --umask umask value to use while running backup process --v, --verbose show info messages Run a backup, creating a snapshot in the process. @@ -30,14 +29,13 @@ and import argparse import logging import os -import sys import rbackup.config.config_files as config +import rbackup.logging import rbackup.script as script from rbackup.struct.repository import Repository # ========== Constants ========== -LOGFORMAT = "==> %(levelname)s %(message)s" EXTRA_RSYNC_OPTS = { "dry_run": "--dry-run", "delete": "--delete-after", @@ -50,22 +48,9 @@ E_PERMISSION = 13 # ========== Logging Setup ========== -console_formatter = logging.Formatter(LOGFORMAT) syslog = logging.getLogger("rbackup") syslog.setLevel(logging.DEBUG) -stdout_handler = logging.StreamHandler(sys.stdout) -stdout_handler.setLevel(logging.INFO) -stdout_handler.setFormatter(console_formatter) -stdout_handler.addFilter(lambda record: record.levelno <= logging.INFO) - -stderr_handler = logging.StreamHandler(sys.stderr) -stderr_handler.setLevel(logging.WARNING) -stderr_handler.setFormatter(console_formatter) - -syslog.addHandler(stdout_handler) -syslog.addHandler(stderr_handler) - # ========== Functions ========== def parse_cmdline_arguments(**kwargs): @@ -132,11 +117,11 @@ def parse_cmdline_arguments(**kwargs): if __name__ == "__main__": args = parse_cmdline_arguments() - if args.verbose: - stdout_handler.setLevel(logging.INFO) - - if args.debug: - stdout_handler.setLevel(logging.DEBUG) + stdout_handler, stderr_handler = rbackup.logging.retrieve_console_handlers( + debug=args.debug + ) + syslog.addHandler(stdout_handler) + syslog.addHandler(stderr_handler) dest = ( args.repository diff --git a/rbackup/logging.py b/rbackup/logging.py new file mode 100644 index 0000000..3158720 --- /dev/null +++ b/rbackup/logging.py @@ -0,0 +1,37 @@ +""" +.. moduleauthor:: Eric Torres +.. module:: rbackup.logging + :synopsis: library for common logging constants +""" + +import logging +import sys + +# ========== Constants ========== +# ----- Console Formatters ----- +_CONSOLE_FORMAT = "==> %(levelname)s %(message)s" +CONSOLE_FORMATTER = logging.Formatter(_CONSOLE_FORMAT) + + +# ========== Functions ========== +def retrieve_console_handlers(*, debug=False): + """Retrieve a pair of logging handlers configured for console output. + + :param debug: should logging.DEBUG level messages be recorded? + :type debug: bool + :return: tuple of ``logging.StreamHandler``s for stdout and stderr + """ + stdout_handler = logging.StreamHandler(sys.stdout) + stdout_handler.setFormatter(_CONSOLE_FORMAT) + stdout_handler.addFilter(lambda record: record.levelno <= logging.INFO) + + if debug: + stdout_handler.setLevel(logging.DEBUG) + else: + stdout_handler.setLevel(logging.INFO) + + stderr_handler = logging.StreamHandler(sys.stderr) + stderr_handler.setLevel(logging.WARNING) + stderr_handler.setFormatter(_CONSOLE_FORMAT) + + return stdout_handler, stderr_handler