From b3aa6af85961588c42b4afc34db1bb2861e84563 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 17 Jun 2019 11:53:08 -0700 Subject: [PATCH] Don't color syslog output (#197). --- NEWS | 1 + borgmatic/borg/check.py | 3 +- borgmatic/borg/create.py | 3 +- borgmatic/borg/extract.py | 3 +- borgmatic/borg/info.py | 3 +- borgmatic/borg/init.py | 3 +- borgmatic/borg/list.py | 3 +- borgmatic/borg/prune.py | 3 +- borgmatic/commands/borgmatic.py | 4 +-- borgmatic/commands/validate_config.py | 3 +- borgmatic/config/load.py | 5 ++- borgmatic/config/validate.py | 3 +- borgmatic/execute.py | 4 +-- borgmatic/hook.py | 3 +- borgmatic/logger.py | 50 +++------------------------ tests/unit/test_logger.py | 23 ++++++------ 16 files changed, 30 insertions(+), 87 deletions(-) diff --git a/NEWS b/NEWS index 820239b..3d085df 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 ab83589..f343157 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 11abac4..bb0a138 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 4dd0cd0..b00fe68 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 7bd395e..6460a7b 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 ff91f73..f3f9e8a 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 541aca3..c01f963 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 05f7bb0..8525f54 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 25ed2c3..0d553ba 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 6ad446b..00ea9f4 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 b2c2e27..b4dd141 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 8c92c47..5a28633 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 38fd71e..1458247 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 899f630..d3b64ac 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 334930c..439f09f 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 80a3b7e..c1e538b 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 )