Selaa lähdekoodia

new option for log-file

palto42 5 vuotta sitten
vanhempi
sitoutus
22640a9ca0
3 muutettua tiedostoa jossa 15 lisäystä ja 4 poistoa
  1. 6 0
      borgmatic/commands/arguments.py
  2. 1 0
      borgmatic/commands/borgmatic.py
  3. 8 4
      borgmatic/logger.py

+ 6 - 0
borgmatic/commands/arguments.py

@@ -148,6 +148,12 @@ def parse_arguments(*unparsed_arguments):
         default=0,
         default=0,
         help='Display verbose progress to syslog (from none to lots: 0, 1, or 2). Ignored when console is interactive',
         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(
     global_group.add_argument(
         '--version',
         '--version',
         dest='version',
         dest='version',

+ 1 - 0
borgmatic/commands/borgmatic.py

@@ -433,6 +433,7 @@ def main():  # pragma: no cover
     configure_logging(
     configure_logging(
         verbosity_to_log_level(global_arguments.verbosity),
         verbosity_to_log_level(global_arguments.verbosity),
         verbosity_to_log_level(global_arguments.syslog_verbosity),
         verbosity_to_log_level(global_arguments.syslog_verbosity),
+        global_arguments.log_file,
     )
     )
 
 
     logger.debug('Ensuring legacy configuration is upgraded')
     logger.debug('Ensuring legacy configuration is upgraded')

+ 8 - 4
borgmatic/logger.py

@@ -73,15 +73,19 @@ def color_text(color, message):
     return '{}{}{}'.format(color, message, colorama.Style.RESET_ALL)
     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.
     Configure logging to go to both the console and syslog. Use the given log levels, respectively.
     '''
     '''
     if syslog_log_level is None:
     if syslog_log_level is None:
         syslog_log_level = console_log_level
         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)
     console_handler.setLevel(console_log_level)
 
 
     syslog_path = None
     syslog_path = None
@@ -90,7 +94,7 @@ def configure_logging(console_log_level, syslog_log_level=None):
     elif os.path.exists('/var/run/syslog'):
     elif os.path.exists('/var/run/syslog'):
         syslog_path = '/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 = logging.handlers.SysLogHandler(address=syslog_path)
         syslog_handler.setFormatter(logging.Formatter('borgmatic: %(levelname)s %(message)s'))
         syslog_handler.setFormatter(logging.Formatter('borgmatic: %(levelname)s %(message)s'))
         syslog_handler.setLevel(syslog_log_level)
         syslog_handler.setLevel(syslog_log_level)