Example #1
0
def test_configure_logging_skips_syslog_if_not_found():
    flexmock(module).should_receive('Console_color_formatter')
    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.handlers).should_receive('SysLogHandler').never()

    module.configure_logging(console_log_level=logging.INFO)
Example #2
0
def test_configure_logging_sets_global_logger_to_most_verbose_log_level():
    flexmock(module).should_receive('Console_color_formatter')
    flexmock(module.logging).should_receive('basicConfig').with_args(
        level=logging.DEBUG, handlers=tuple).once()
    flexmock(module.os.path).should_receive('exists').and_return(False)

    module.configure_logging(console_log_level=logging.INFO,
                             syslog_log_level=logging.DEBUG)
Example #3
0
def test_configure_logging_probes_for_log_socket_on_linux():
    flexmock(module).should_receive('Console_color_formatter')
    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').with_args(
        '/dev/log').and_return(True)
    syslog_handler = logging.handlers.SysLogHandler()
    flexmock(
        module.logging.handlers).should_receive('SysLogHandler').with_args(
            address='/dev/log').and_return(syslog_handler).once()

    module.configure_logging(logging.INFO)
Example #4
0
def test_configure_logging_skips_syslog_if_interactive_console():
    flexmock(module).should_receive('Multi_stream_handler').and_return(
        flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
    )
    flexmock(module).should_receive('Console_color_formatter')
    flexmock(module).should_receive('interactive_console').and_return(True)
    flexmock(module.logging).should_receive('basicConfig').with_args(
        level=logging.INFO, handlers=tuple
    )
    flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
    flexmock(module.logging.handlers).should_receive('SysLogHandler').never()

    module.configure_logging(console_log_level=logging.INFO)
Example #5
0
def test_configure_logging_skips_logfile_if_argument_is_none():
    flexmock(module).should_receive('Multi_stream_handler').and_return(
        flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: 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.handlers).should_receive('WatchedFileHandler').never()

    module.configure_logging(console_log_level=logging.INFO, log_file=None)
Example #6
0
def test_configure_logging_probes_for_log_socket_on_macos():
    flexmock(module).should_receive('Multi_stream_handler').and_return(
        flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
    )
    flexmock(module).should_receive('Console_color_formatter')
    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').with_args('/dev/log').and_return(False)
    flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
    syslog_handler = logging.handlers.SysLogHandler()
    flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
        address='/var/run/syslog'
    ).and_return(syslog_handler).once()

    module.configure_logging(logging.INFO)
Example #7
0
def test_configure_logging_to_logfile_instead_of_syslog():
    flexmock(module).should_receive('Multi_stream_handler').and_return(
        flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
    )

    # syslog skipped in non-interactive console if --log-file argument provided
    flexmock(module).should_receive('interactive_console').and_return(False)
    flexmock(module.logging).should_receive('basicConfig').with_args(
        level=logging.DEBUG, handlers=tuple
    )
    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.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'
    )
Example #8
0
def main():  # pragma: no cover
    configure_signals()

    try:
        arguments = parse_arguments(*sys.argv[1:])
    except ValueError as error:
        configure_logging(logging.CRITICAL)
        logger.critical(error)
        exit_with_help_link()
    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()

    global_arguments = arguments['global']
    if global_arguments.version:
        print(pkg_resources.require('borgmatic')[0].version)
        sys.exit(0)

    config_filenames = tuple(
        collect.collect_config_filenames(global_arguments.config_paths))
    configs, parse_logs = load_configurations(config_filenames)

    colorama.init(
        autoreset=True,
        strip=not should_do_markup(global_arguments.no_color, configs))
    configure_logging(
        verbosity_to_log_level(global_arguments.verbosity),
        verbosity_to_log_level(global_arguments.syslog_verbosity),
    )

    logger.debug('Ensuring legacy configuration is upgraded')
    convert.guard_configuration_upgraded(LEGACY_CONFIG_PATH, config_filenames)

    summary_logs = list(
        collect_configuration_run_summary_logs(configs, arguments))

    logger.info('')
    logger.info('summary:')
    [
        logger.handle(log) for log in parse_logs + summary_logs
        if log.levelno >= logger.getEffectiveLevel()
    ]

    if any(log.levelno == logging.CRITICAL for log in summary_logs):
        exit_with_help_link()
Example #9
0
def main():  # pragma: no cover
    configure_signals()

    try:
        arguments = parse_arguments(*sys.argv[1:])
    except ValueError as error:
        configure_logging(logging.CRITICAL)
        logger.critical(error)
        exit_with_help_link()
    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()

    global_arguments = arguments['global']
    if global_arguments.version:
        print(pkg_resources.require('borgmatic')[0].version)
        sys.exit(0)

    config_filenames = tuple(
        collect.collect_config_filenames(global_arguments.config_paths))
    configs, parse_logs = load_configurations(config_filenames,
                                              global_arguments.overrides)

    any_json_flags = any(
        getattr(sub_arguments, 'json', False)
        for sub_arguments in arguments.values())
    colorama.init(
        autoreset=True,
        strip=not should_do_markup(global_arguments.no_color or any_json_flags,
                                   configs),
    )
    try:
        configure_logging(
            verbosity_to_log_level(global_arguments.verbosity),
            verbosity_to_log_level(global_arguments.syslog_verbosity),
            verbosity_to_log_level(global_arguments.log_file_verbosity),
            verbosity_to_log_level(global_arguments.monitoring_verbosity),
            global_arguments.log_file,
        )
    except (FileNotFoundError, PermissionError) as error:
        configure_logging(logging.CRITICAL)
        logger.critical('Error configuring logging: {}'.format(error))
        exit_with_help_link()

    logger.debug('Ensuring legacy configuration is upgraded')
    convert.guard_configuration_upgraded(LEGACY_CONFIG_PATH, config_filenames)

    summary_logs = parse_logs + list(
        collect_configuration_run_summary_logs(configs, arguments))
    summary_logs_max_level = max(log.levelno for log in summary_logs)

    for message in ('', 'summary:'):
        log_record(
            levelno=summary_logs_max_level,
            levelname=logging.getLevelName(summary_logs_max_level),
            msg=message,
        )

    for log in summary_logs:
        logger.handle(log)

    if summary_logs_max_level >= logging.CRITICAL:
        exit_with_help_link()