1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-01-01 12:45:34 +00:00

remote repos: remove support for borg < 1.1.0 ($LOG, logging setup)

log output:

always expect json, remove $LOG format support.

we keep limited support for unstructured format also,
just not to lose anything from remote stderr.
This commit is contained in:
Thomas Waldmann 2023-05-24 01:14:31 +02:00
parent 51177b9f06
commit 4b796a7337
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01
4 changed files with 22 additions and 63 deletions

View file

@ -482,8 +482,8 @@ def run(self, args):
func = get_func(args)
# do not use loggers before this!
is_serve = func == self.do_serve
setup_logging(level=args.log_level, is_serve=is_serve, json=args.log_json)
self.log_json = args.log_json
self.log_json = args.log_json or is_serve
setup_logging(level=args.log_level, json=self.log_json)
args.progress |= is_serve
self._setup_implied_logging(vars(args))
self._setup_topic_debugging(args)

View file

@ -53,7 +53,7 @@ def _log_warning(message, category, filename, lineno, file=None, line=None):
logger.warning(msg)
def setup_logging(stream=None, conf_fname=None, env_var="BORG_LOGGING_CONF", level="info", is_serve=False, json=False):
def setup_logging(stream=None, conf_fname=None, env_var="BORG_LOGGING_CONF", level="info", json=False):
"""setup logging module according to the arguments provided
if conf_fname is given (or the config file name can be determined via
@ -61,9 +61,6 @@ def setup_logging(stream=None, conf_fname=None, env_var="BORG_LOGGING_CONF", lev
otherwise, set up a stream handler logger on stderr (by default, if no
stream is provided).
if is_serve == True, we configure a special log format as expected by
the borg client log message interceptor.
"""
global configured
err_msg = None
@ -90,21 +87,12 @@ def setup_logging(stream=None, conf_fname=None, env_var="BORG_LOGGING_CONF", lev
# if we did not / not successfully load a logging configuration, fallback to this:
logger = logging.getLogger("")
handler = logging.StreamHandler(stream)
if is_serve and not json:
fmt = "$LOG %(levelname)s %(name)s Remote: %(message)s"
else:
fmt = "%(message)s"
fmt = "%(message)s"
formatter = JsonFormatter(fmt) if json else logging.Formatter(fmt)
handler.setFormatter(formatter)
borg_logger = logging.getLogger("borg")
borg_logger.formatter = formatter
borg_logger.json = json
if configured and logger.handlers:
# The RepositoryServer can call setup_logging a second time to adjust the output
# mode from text-ish is_serve to json is_serve.
# Thus, remove the previously installed handler, if any.
logger.handlers[0].close()
logger.handlers.clear()
logger.addHandler(handler)
logger.setLevel(level.upper())
configured = True

View file

@ -27,7 +27,7 @@
from .helpers import format_file_size
from .helpers import safe_unlink
from .helpers import prepare_subprocess_env, ignore_sigint
from .logger import create_logger, setup_logging
from .logger import create_logger
from .helpers import msgpack
from .repository import Repository
from .version import parse_version, format_version
@ -244,9 +244,6 @@ def serve(self):
def negotiate(self, client_data):
if isinstance(client_data, dict):
self.client_version = client_data["client_version"]
level = logging.getLevelName(logging.getLogger("").level)
setup_logging(is_serve=True, json=True, level=level)
logger.debug("Initialized logging system for JSON-based protocol")
else:
self.client_version = BORG_VERSION # seems to be newer than current version (no known old format)
@ -897,8 +894,6 @@ def handle_remote_line(line):
"""
assert line.endswith(("\r", "\n"))
if line.startswith("{"):
# This format is used by Borg since 1.1.0b6 for new-protocol clients.
# It is the same format that is exposed by --log-json.
msg = json.loads(line)
if msg["type"] not in ("progress_message", "progress_percent", "log_message"):
@ -937,30 +932,9 @@ def handle_remote_line(line):
# so that the next message, progress or not, overwrites it. This mirrors the behaviour
# of local progress displays.
sys.stderr.write("Remote: " + msg["message"] + "\r")
elif line.startswith("$LOG "):
# This format is used by borg serve 0.xx, 1.0.x and 1.1.0b1..b5.
# It prefixed log lines with $LOG as a marker, followed by the log level
# and optionally a logger name, then "Remote:" as a separator followed by the original
# message.
_, level, msg = line.split(" ", 2)
level = getattr(logging, level, logging.CRITICAL) # str -> int
if msg.startswith("Remote:"):
# server format: '$LOG <level> Remote: <msg>'
logging.log(level, msg.rstrip())
else:
# server format '$LOG <level> <logname> Remote: <msg>'
logname, msg = msg.split(" ", 1)
logging.getLogger(logname).log(level, msg.rstrip())
else:
# Plain 1.0.x and older format - re-emit to stderr (mirroring what the 1.0.x
# client did) or as a generic log message.
# We don't know what priority the line had.
if logging.getLogger("borg").json:
logging.getLogger("").warning("Remote: " + line.strip())
else:
# In non-JSON mode we circumvent logging to preserve carriage returns (\r)
# which are generated by remote progress displays.
sys.stderr.write("Remote: " + line)
logging.getLogger("").warning("stderr/remote: " + line.strip())
class RepositoryNoCache:

View file

@ -1083,29 +1083,18 @@ def tearDown(self):
def test_stderr_messages(self):
handle_remote_line("unstructured stderr message\n")
self.assert_equal(self.stream.getvalue(), "")
# stderr messages don't get an implicit newline
self.assert_equal(self.stderr.getvalue(), "Remote: unstructured stderr message\n")
def test_stderr_progress_messages(self):
handle_remote_line("unstructured stderr progress message\r")
self.assert_equal(self.stream.getvalue(), "")
# stderr messages don't get an implicit newline
self.assert_equal(self.stderr.getvalue(), "Remote: unstructured stderr progress message\r")
def test_pre11_format_messages(self):
self.handler.setLevel(logging.DEBUG)
logging.getLogger().setLevel(logging.DEBUG)
handle_remote_line("$LOG INFO Remote: borg < 1.1 format message\n")
self.assert_equal(self.stream.getvalue(), "Remote: borg < 1.1 format message\n")
self.assert_equal(self.stream.getvalue(), "stderr/remote: unstructured stderr message\n")
self.assert_equal(self.stderr.getvalue(), "")
def test_post11_format_messages(self):
self.handler.setLevel(logging.DEBUG)
logging.getLogger().setLevel(logging.DEBUG)
handle_remote_line("$LOG INFO borg.repository Remote: borg >= 1.1 format message\n")
msg = (
"""{"type": "log_message", "levelname": "INFO", "name": "borg.repository", "msgid": 42,"""
""" "message": "borg >= 1.1 format message"}\n"""
)
handle_remote_line(msg)
self.assert_equal(self.stream.getvalue(), "Remote: borg >= 1.1 format message\n")
self.assert_equal(self.stderr.getvalue(), "")
@ -1114,7 +1103,11 @@ def test_remote_messages_screened(self):
self.handler.setLevel(logging.WARNING)
logging.getLogger().setLevel(logging.WARNING)
handle_remote_line("$LOG INFO borg.repository Remote: new format info message\n")
msg = (
"""{"type": "log_message", "levelname": "INFO", "name": "borg.repository", "msgid": 42,"""
""" "message": "new format info message"}\n"""
)
handle_remote_line(msg)
self.assert_equal(self.stream.getvalue(), "")
self.assert_equal(self.stderr.getvalue(), "")
@ -1134,7 +1127,11 @@ def test_info_to_correct_local_child(self):
foo_handler.setLevel(logging.INFO)
logging.getLogger("borg.repository.foo").handlers[:] = [foo_handler]
handle_remote_line("$LOG INFO borg.repository Remote: new format child message\n")
msg = (
"""{"type": "log_message", "levelname": "INFO", "name": "borg.repository", "msgid": 42,"""
""" "message": "new format child message"}\n"""
)
handle_remote_line(msg)
self.assert_equal(foo_stream.getvalue(), "")
self.assert_equal(child_stream.getvalue(), "Remote: new format child message\n")
self.assert_equal(self.stream.getvalue(), "")