From 9ceeae2de055dc1784e030122ff77470228c2812 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 27 May 2019 15:44:48 -0700 Subject: [PATCH] Add separate syslog verbosity flag. --- borgmatic/commands/borgmatic.py | 17 ++++++++++++++--- borgmatic/logger.py | 18 +++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index f399f0ec7..342dee3a5 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -176,7 +176,14 @@ def parse_arguments(*arguments): type=int, choices=range(0, 3), default=0, - help='Display verbose progress (1 for some, 2 for lots)', + help='Display verbose progress to the console (from none to lots: 0, 1, or 2)', + ) + common_group.add_argument( + '--syslog-verbosity', + type=int, + choices=range(0, 3), + default=2, + help='Display verbose progress to syslog (from none to lots: 0, 1, or 2)', ) common_group.add_argument( '--version', @@ -483,14 +490,18 @@ def main(): # pragma: no cover configure_logging(logging.CRITICAL) logger.critical(error) exit_with_help_link() - except SystemExit: + except SystemExit as error: + if error.code == 0: + raise error 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)) - configure_logging(verbosity_to_log_level(args.verbosity)) + configure_logging( + verbosity_to_log_level(args.verbosity), verbosity_to_log_level(args.syslog_verbosity) + ) if args.version: print(pkg_resources.require('borgmatic')[0].version) diff --git a/borgmatic/logger.py b/borgmatic/logger.py index 994193f84..37f20ec5f 100644 --- a/borgmatic/logger.py +++ b/borgmatic/logger.py @@ -103,14 +103,22 @@ def color_text(color, message): return '{}{}{}'.format(color, message, colorama.Style.RESET_ALL) -def configure_logging(log_level): +def configure_logging(console_log_level, syslog_log_level=None): ''' - Configure logging to go to both the console and syslog. Use the given log level for both. + Configure logging to go to both the console and syslog. Use the given log levels, respectively. ''' + if syslog_log_level is None: + syslog_log_level = console_log_level + + console_handler = logging.StreamHandler() + console_handler.setFormatter(logging.Formatter('%(message)s')) + console_handler.setLevel(console_log_level) + syslog_handler = logging.handlers.SysLogHandler(address='/dev/log') syslog_handler.setFormatter(logging.Formatter('borgmatic: %(levelname)s %(message)s')) + syslog_handler.setLevel(syslog_log_level) + logging.basicConfig( - level=log_level, - format='%(message)s', - handlers=(logging.StreamHandler(), syslog_handler), + level=min(console_log_level, syslog_log_level), + handlers=(console_handler, syslog_handler), )