new option for log-file

This commit is contained in:
palto42 2019-10-31 10:44:22 +01:00
parent ca23c3b8b3
commit 22640a9ca0
3 changed files with 15 additions and 4 deletions

View file

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

View file

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

View file

@ -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() if log_file is None:
console_handler.setFormatter(Console_color_formatter()) 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)