Add separate syslog verbosity flag.

This commit is contained in:
Dan Helfman 2019-05-27 15:44:48 -07:00
parent e0e493c2f1
commit 9ceeae2de0
2 changed files with 27 additions and 8 deletions

View file

@ -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)

View file

@ -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),
)