From 0a9f4e8708f18e73341e58a5a55403bcab3cfc34 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Tue, 12 Nov 2019 16:13:25 -0800 Subject: [PATCH] Reopen the file given by "--log-file" flag if an external program rotates the log file while borgmatic is running. --- NEWS | 3 ++- borgmatic/logger.py | 2 +- tests/unit/test_logger.py | 12 ++++++------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 25379ebc..2bbc2ef4 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ 1.4.10.dev0 - * + * Reopen the file given by "--log-file" flag if an external program rotates the log file while + borgmatic is running. 1.4.9 * #228: Database dump hooks for MySQL/MariaDB, so you can easily dump your databases before backups diff --git a/borgmatic/logger.py b/borgmatic/logger.py index 51572974..ce9be04a 100644 --- a/borgmatic/logger.py +++ b/borgmatic/logger.py @@ -104,7 +104,7 @@ def configure_logging( syslog_handler.setLevel(syslog_log_level) handlers = (console_handler, syslog_handler) elif log_file: - file_handler = logging.FileHandler(log_file) + file_handler = logging.handlers.WatchedFileHandler(log_file) file_handler.setFormatter(logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s')) file_handler.setLevel(log_file_log_level) handlers = (console_handler, file_handler) diff --git a/tests/unit/test_logger.py b/tests/unit/test_logger.py index 91b25541..8eeb6be6 100644 --- a/tests/unit/test_logger.py +++ b/tests/unit/test_logger.py @@ -203,10 +203,10 @@ def test_configure_logging_to_logfile_instead_of_syslog(): ) flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True) flexmock(module.logging.handlers).should_receive('SysLogHandler').never() - file_handler = logging.FileHandler('/tmp/logfile') - flexmock(module.logging).should_receive('FileHandler').with_args('/tmp/logfile').and_return( - file_handler - ).once() + file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile') + flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args( + '/tmp/logfile' + ).and_return(file_handler).once() module.configure_logging( console_log_level=logging.INFO, log_file_log_level=logging.DEBUG, log_file='/tmp/logfile' @@ -214,12 +214,12 @@ def test_configure_logging_to_logfile_instead_of_syslog(): def test_configure_logging_skips_logfile_if_argument_is_none(): - # No FileHandler added if argument --log-file is None + # No WatchedFileHandler added if argument --log-file is None flexmock(module).should_receive('interactive_console').and_return(False) flexmock(module.logging).should_receive('basicConfig').with_args( level=logging.INFO, handlers=tuple ) flexmock(module.os.path).should_receive('exists').and_return(False) - flexmock(module.logging).should_receive('FileHandler').never() + flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never() module.configure_logging(console_log_level=logging.INFO, log_file=None)