diff --git a/NEWS b/NEWS index 820239b80..3d085df6e 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ 1.3.7.dev0 * #196: Fix for unclear error message for invalid YAML merge include. + * #197: Don't color syslog output. * Change default syslog verbosity to show errors only. 1.3.6 diff --git a/borgmatic/borg/check.py b/borgmatic/borg/check.py index ab8358913..f34315797 100644 --- a/borgmatic/borg/check.py +++ b/borgmatic/borg/check.py @@ -2,13 +2,12 @@ import logging from borgmatic.borg import extract from borgmatic.execute import execute_command -from borgmatic.logger import get_logger DEFAULT_CHECKS = ('repository', 'archives') DEFAULT_PREFIX = '{hostname}-' -logger = get_logger(__name__) +logger = logging.getLogger(__name__) def _parse_checks(consistency_config): diff --git a/borgmatic/borg/create.py b/borgmatic/borg/create.py index 11abac48c..bb0a13899 100644 --- a/borgmatic/borg/create.py +++ b/borgmatic/borg/create.py @@ -5,9 +5,8 @@ import os import tempfile from borgmatic.execute import execute_command -from borgmatic.logger import get_logger -logger = get_logger(__name__) +logger = logging.getLogger(__name__) def _expand_directory(directory): diff --git a/borgmatic/borg/extract.py b/borgmatic/borg/extract.py index 4dd0cd071..b00fe6879 100644 --- a/borgmatic/borg/extract.py +++ b/borgmatic/borg/extract.py @@ -1,9 +1,8 @@ import logging from borgmatic.execute import execute_command -from borgmatic.logger import get_logger -logger = get_logger(__name__) +logger = logging.getLogger(__name__) def extract_last_archive_dry_run(repository, lock_wait=None, local_path='borg', remote_path=None): diff --git a/borgmatic/borg/info.py b/borgmatic/borg/info.py index 7bd395ece..6460a7bfa 100644 --- a/borgmatic/borg/info.py +++ b/borgmatic/borg/info.py @@ -1,9 +1,8 @@ import logging from borgmatic.execute import execute_command -from borgmatic.logger import get_logger -logger = get_logger(__name__) +logger = logging.getLogger(__name__) def display_archives_info( diff --git a/borgmatic/borg/init.py b/borgmatic/borg/init.py index ff91f73be..f3f9e8a9f 100644 --- a/borgmatic/borg/init.py +++ b/borgmatic/borg/init.py @@ -2,9 +2,8 @@ import logging import subprocess from borgmatic.execute import execute_command -from borgmatic.logger import get_logger -logger = get_logger(__name__) +logger = logging.getLogger(__name__) INFO_REPOSITORY_NOT_FOUND_EXIT_CODE = 2 diff --git a/borgmatic/borg/list.py b/borgmatic/borg/list.py index 541aca3be..c01f96354 100644 --- a/borgmatic/borg/list.py +++ b/borgmatic/borg/list.py @@ -1,9 +1,8 @@ import logging from borgmatic.execute import execute_command -from borgmatic.logger import get_logger -logger = get_logger(__name__) +logger = logging.getLogger(__name__) def list_archives( diff --git a/borgmatic/borg/prune.py b/borgmatic/borg/prune.py index 05f7bb0d4..8525f54ce 100644 --- a/borgmatic/borg/prune.py +++ b/borgmatic/borg/prune.py @@ -1,9 +1,8 @@ import logging from borgmatic.execute import execute_command -from borgmatic.logger import get_logger -logger = get_logger(__name__) +logger = logging.getLogger(__name__) def _make_prune_flags(retention_config): diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 25ed2c339..0d553ba46 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -19,11 +19,11 @@ from borgmatic.borg import init as borg_init from borgmatic.borg import list as borg_list from borgmatic.borg import prune as borg_prune from borgmatic.config import checks, collect, convert, validate -from borgmatic.logger import configure_logging, get_logger, should_do_markup +from borgmatic.logger import configure_logging, should_do_markup from borgmatic.signals import configure_signals from borgmatic.verbosity import verbosity_to_log_level -logger = get_logger(__name__) +logger = logging.getLogger(__name__) LEGACY_CONFIG_PATH = '/etc/borgmatic/config' diff --git a/borgmatic/commands/validate_config.py b/borgmatic/commands/validate_config.py index 6ad446b20..00ea9f45f 100644 --- a/borgmatic/commands/validate_config.py +++ b/borgmatic/commands/validate_config.py @@ -3,9 +3,8 @@ import sys from argparse import ArgumentParser from borgmatic.config import collect, validate -from borgmatic.logger import get_logger -logger = get_logger(__name__) +logger = logging.getLogger(__name__) def parse_arguments(*arguments): diff --git a/borgmatic/config/load.py b/borgmatic/config/load.py index b2c2e272f..b4dd141a0 100644 --- a/borgmatic/config/load.py +++ b/borgmatic/config/load.py @@ -1,10 +1,9 @@ +import logging import os import ruamel.yaml -from borgmatic.logger import get_logger - -logger = get_logger(__name__) +logger = logging.getLogger(__name__) def load_configuration(filename): diff --git a/borgmatic/config/validate.py b/borgmatic/config/validate.py index 8c92c470e..5a28633e5 100644 --- a/borgmatic/config/validate.py +++ b/borgmatic/config/validate.py @@ -6,9 +6,8 @@ import pykwalify.errors import ruamel.yaml from borgmatic.config import load -from borgmatic.logger import get_logger -logger = get_logger(__name__) +logger = logging.getLogger(__name__) def schema_filename(): diff --git a/borgmatic/execute.py b/borgmatic/execute.py index 38fd71ecf..1458247ce 100644 --- a/borgmatic/execute.py +++ b/borgmatic/execute.py @@ -1,9 +1,7 @@ import logging import subprocess -from borgmatic.logger import get_logger - -logger = get_logger(__name__) +logger = logging.getLogger(__name__) def execute_and_log_output(full_command, output_log_level, shell): diff --git a/borgmatic/hook.py b/borgmatic/hook.py index 899f63013..d3b64ac68 100644 --- a/borgmatic/hook.py +++ b/borgmatic/hook.py @@ -2,9 +2,8 @@ import logging import os from borgmatic import execute -from borgmatic.logger import get_logger -logger = get_logger(__name__) +logger = logging.getLogger(__name__) def execute_hook(commands, umask, config_filename, description, dry_run): diff --git a/borgmatic/logger.py b/borgmatic/logger.py index 334930c4b..439f09f6d 100644 --- a/borgmatic/logger.py +++ b/borgmatic/logger.py @@ -45,52 +45,10 @@ LOG_LEVEL_TO_COLOR = { } -class Borgmatic_logger(logging.Logger): - def critical(self, msg, *args, **kwargs): - color = LOG_LEVEL_TO_COLOR.get(logging.CRITICAL) - - return super(Borgmatic_logger, self).critical(color_text(color, msg), *args, **kwargs) - - def error(self, msg, *args, **kwargs): - color = LOG_LEVEL_TO_COLOR.get(logging.ERROR) - - return super(Borgmatic_logger, self).error(color_text(color, msg), *args, **kwargs) - - def warn(self, msg, *args, **kwargs): - color = LOG_LEVEL_TO_COLOR.get(logging.WARN) - - return super(Borgmatic_logger, self).warn(color_text(color, msg), *args, **kwargs) - - def info(self, msg, *args, **kwargs): - color = LOG_LEVEL_TO_COLOR.get(logging.INFO) - - return super(Borgmatic_logger, self).info(color_text(color, msg), *args, **kwargs) - - def debug(self, msg, *args, **kwargs): - color = LOG_LEVEL_TO_COLOR.get(logging.DEBUG) - - return super(Borgmatic_logger, self).debug(color_text(color, msg), *args, **kwargs) - - def handle(self, record): +class Console_color_formatter(logging.Formatter): + def format(self, record): color = LOG_LEVEL_TO_COLOR.get(record.levelno) - colored_record = logging.makeLogRecord( - dict( - levelno=record.levelno, - levelname=record.levelname, - msg=color_text(color, record.msg), - ) - ) - - return super(Borgmatic_logger, self).handle(colored_record) - - -def get_logger(name=None): - ''' - Build a logger with the given name. - ''' - logging.setLoggerClass(Borgmatic_logger) - logger = logging.getLogger(name) - return logger + return color_text(color, record.msg) def color_text(color, message): @@ -111,7 +69,7 @@ def configure_logging(console_log_level, syslog_log_level=None): syslog_log_level = console_log_level console_handler = logging.StreamHandler() - console_handler.setFormatter(logging.Formatter('%(message)s')) + console_handler.setFormatter(Console_color_formatter()) console_handler.setLevel(console_log_level) syslog_path = None diff --git a/tests/unit/test_logger.py b/tests/unit/test_logger.py index 80a3b7e67..c1e538b2d 100644 --- a/tests/unit/test_logger.py +++ b/tests/unit/test_logger.py @@ -51,21 +51,14 @@ def test_should_do_markup_prefers_PY_COLORS_to_stdout_tty_value(): assert module.should_do_markup(no_color=False) is True -@pytest.mark.parametrize('method_name', ('critical', 'error', 'warn', 'info', 'debug')) -def test_borgmatic_logger_log_method_does_not_raise(method_name): - flexmock(module).should_receive('color_text') - flexmock(module.logging.Logger).should_receive(method_name) +def test_console_color_formatter_format_includes_log_message(): + plain_message = 'uh oh' + record = flexmock(levelno=logging.CRITICAL, msg=plain_message) - getattr(module.Borgmatic_logger('test'), method_name)(msg='hi') + colored_message = module.Console_color_formatter().format(record) - -def test_borgmatic_logger_handle_does_not_raise(): - flexmock(module).should_receive('color_text') - flexmock(module.logging.Logger).should_receive('handle') - - module.Borgmatic_logger('test').handle( - module.logging.makeLogRecord(dict(levelno=module.logging.CRITICAL, msg='hi')) - ) + assert colored_message != plain_message + assert plain_message in colored_message def test_color_text_does_not_raise(): @@ -77,6 +70,7 @@ def test_color_text_without_color_does_not_raise(): def test_configure_logging_probes_for_log_socket_on_linux(): + flexmock(module).should_receive('Console_color_formatter') flexmock(module.logging).should_receive('basicConfig').with_args( level=logging.INFO, handlers=tuple ) @@ -91,6 +85,7 @@ def test_configure_logging_probes_for_log_socket_on_linux(): def test_configure_logging_probes_for_log_socket_on_macos(): + flexmock(module).should_receive('Console_color_formatter') flexmock(module.logging).should_receive('basicConfig').with_args( level=logging.INFO, handlers=tuple ) @@ -105,6 +100,7 @@ def test_configure_logging_probes_for_log_socket_on_macos(): 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() @@ -114,6 +110,7 @@ def test_configure_logging_sets_global_logger_to_most_verbose_log_level(): 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 )