From e0e493c2f164feb099b5e6ed120221d6d8ed1ce2 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 27 May 2019 15:05:26 -0700 Subject: [PATCH] Factor out configuring of logging into a common function. --- borgmatic/commands/borgmatic.py | 22 +++++++++++----------- borgmatic/logger.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 5657614..f399f0e 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -19,7 +19,7 @@ from borgmatic.borg import list as borg_list from borgmatic.borg import prune as borg_prune from borgmatic.commands import hook from borgmatic.config import checks, collect, convert, validate -from borgmatic.logger import get_logger, should_do_markup +from borgmatic.logger import get_logger, should_do_markup, configure_logging from borgmatic.signals import configure_signals from borgmatic.verbosity import verbosity_to_log_level @@ -469,7 +469,8 @@ def exit_with_help_link(): # pragma: no cover ''' Display a link to get help and exit with an error code. ''' - logger.critical('\nNeed some help? https://torsion.org/borgmatic/#issues') + logger.critical('') + logger.critical('Need some help? https://torsion.org/borgmatic/#issues') sys.exit(1) @@ -479,19 +480,17 @@ def main(): # pragma: no cover try: args = parse_arguments(*sys.argv[1:]) except ValueError as error: - logging.basicConfig(level=logging.CRITICAL, format='%(message)s') + configure_logging(logging.CRITICAL) logger.critical(error) exit_with_help_link() + except SystemExit: + configure_logging(logging.CRITICAL) + logger.critical('Error parsing arguments: {}'.format(' '.join(sys.argv))) + exit_with_help_link() colorama.init(autoreset=True, strip=not should_do_markup(args.no_color)) - syslog_handler = logging.handlers.SysLogHandler(address='/dev/log') - syslog_handler.setFormatter(logging.Formatter('borgmatic: %(levelname)s %(message)s')) - logging.basicConfig( - level=verbosity_to_log_level(args.verbosity), - format='%(message)s', - handlers=(logging.StreamHandler(), syslog_handler), - ) + configure_logging(verbosity_to_log_level(args.verbosity)) if args.version: print(pkg_resources.require('borgmatic')[0].version) @@ -503,7 +502,8 @@ def main(): # pragma: no cover summary_logs = tuple(collect_configuration_run_summary_logs(config_filenames, args)) - logger.info('\nsummary:') + logger.info('') + logger.info('summary:') [logger.handle(log) for log in summary_logs if log.levelno >= logger.getEffectiveLevel()] if any(log.levelno == logging.CRITICAL for log in summary_logs): diff --git a/borgmatic/logger.py b/borgmatic/logger.py index c704d51..994193f 100644 --- a/borgmatic/logger.py +++ b/borgmatic/logger.py @@ -101,3 +101,16 @@ def color_text(color, message): return message return '{}{}{}'.format(color, message, colorama.Style.RESET_ALL) + + +def configure_logging(log_level): + ''' + Configure logging to go to both the console and syslog. Use the given log level for both. + ''' + syslog_handler = logging.handlers.SysLogHandler(address='/dev/log') + syslog_handler.setFormatter(logging.Formatter('borgmatic: %(levelname)s %(message)s')) + logging.basicConfig( + level=log_level, + format='%(message)s', + handlers=(logging.StreamHandler(), syslog_handler), + )