Don't color syslog output (#197).

This commit is contained in:
Dan Helfman 2019-06-17 11:53:08 -07:00
parent b816af1b13
commit b3aa6af859
16 changed files with 30 additions and 87 deletions

1
NEWS
View file

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

View file

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

View file

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

View file

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

View file

@ -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(

View file

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

View file

@ -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(

View file

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

View file

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

View file

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

View file

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

View file

@ -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():

View file

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

View file

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

View file

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

View file

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