From 22640a9ca0712ae0220e7892169817492fb8a961 Mon Sep 17 00:00:00 2001 From: palto42 <12280188+palto42@users.noreply.github.com> Date: Thu, 31 Oct 2019 10:44:22 +0100 Subject: [PATCH] new option for log-file --- borgmatic/commands/arguments.py | 6 ++++++ borgmatic/commands/borgmatic.py | 1 + borgmatic/logger.py | 12 ++++++++---- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index db92bdcc6..0e72d8ec8 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -148,6 +148,12 @@ def parse_arguments(*unparsed_arguments): default=0, help='Display verbose progress to syslog (from none to lots: 0, 1, or 2). Ignored when console is interactive', ) + global_group.add_argument( + '--log-file', + type=str, + default=None, + help='Write log messages to this file instead of concole and syslog', + ) global_group.add_argument( '--version', dest='version', diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 71abd05f2..7c6139963 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -433,6 +433,7 @@ def main(): # pragma: no cover configure_logging( verbosity_to_log_level(global_arguments.verbosity), verbosity_to_log_level(global_arguments.syslog_verbosity), + global_arguments.log_file, ) logger.debug('Ensuring legacy configuration is upgraded') diff --git a/borgmatic/logger.py b/borgmatic/logger.py index 7bbb912b1..9d14c3d72 100644 --- a/borgmatic/logger.py +++ b/borgmatic/logger.py @@ -73,15 +73,19 @@ def color_text(color, message): return '{}{}{}'.format(color, message, colorama.Style.RESET_ALL) -def configure_logging(console_log_level, syslog_log_level=None): +def configure_logging(console_log_level, syslog_log_level=None, log_file=None): ''' 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(Console_color_formatter()) + if log_file is None: + console_handler = logging.StreamHandler() + console_handler.setFormatter(Console_color_formatter()) + else: + console_handler = logging.FileHandler(log_file) + console_handler.setFormatter(logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s')) console_handler.setLevel(console_log_level) syslog_path = None @@ -90,7 +94,7 @@ def configure_logging(console_log_level, syslog_log_level=None): elif os.path.exists('/var/run/syslog'): syslog_path = '/var/run/syslog' - if syslog_path and not interactive_console(): + if syslog_path and not interactive_console() and log_file is None: syslog_handler = logging.handlers.SysLogHandler(address=syslog_path) syslog_handler.setFormatter(logging.Formatter('borgmatic: %(levelname)s %(message)s')) syslog_handler.setLevel(syslog_log_level)