From 08c69a2463135d51785d252a4ee90b7ac53f95a4 Mon Sep 17 00:00:00 2001 From: aryoda <11374410+aryoda@users.noreply.github.com> Date: Tue, 18 Oct 2022 21:03:38 +0200 Subject: [PATCH] Write all log output to stderr (do not pollute stdout) * Affects logger.info and logger.debug * complies now to the behavior of pythons's standard logging package --- CHANGES | 1 + common/logger.py | 10 ++++++---- common/test/test_backintime.py | 12 +++++++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGES b/CHANGES index 3e3c82ec8..c959d7178 100644 --- a/CHANGES +++ b/CHANGES @@ -5,6 +5,7 @@ Upcoming Release * GUI change: remove Exit button from the toolbar (#172) * GUI change: define accelerator keys for menu bar and tabs, as well as toolbar shortcuts (#1104) * Desktop integration: update .desktop file to mark Back In Time as a single main window program (#1258) +* Improvement: Write all log output to stderr; do not pollute stdout with INFO and WARNING messages anymore (#1337) * Bugfix: AttributeError in "Diff Options" dialog (#898) * Bugfix: Settings GUI: "Save password to Keyring" was disabled due to "no appropriate keyring found" (#1321) * Bugfix: Avoid logging errors while waiting for a target drive to be mounted (#1142, #1143, #1328) diff --git a/common/logger.py b/common/logger.py index 55e7e1dc1..0be94c388 100644 --- a/common/logger.py +++ b/common/logger.py @@ -23,8 +23,8 @@ import tools import bcolors -DEBUG = False -APP_NAME = 'backintime' +DEBUG = False # Set to "True" when passing "--debug" as cmd arg +APP_NAME = 'backintime' # TODO Duplicated code (see Config.APP_NAME) def openlog(): name = os.getenv('LOGNAME', 'unknown') @@ -55,14 +55,16 @@ def warning(msg , parent = None, traceDepth = 0): def info(msg , parent = None, traceDepth = 0): if DEBUG: msg = '%s %s' %(_debugHeader(parent, traceDepth), msg) - print('%sINFO%s: %s' %(bcolors.OKGREEN, bcolors.ENDC, msg), file=sys.stdout) + print('%sINFO%s: %s' %(bcolors.OKGREEN, bcolors.ENDC, msg), file=sys.stderr) for line in tools.wrapLine(msg): syslog.syslog(syslog.LOG_INFO, 'INFO: ' + line) def debug(msg, parent = None, traceDepth = 0): if DEBUG: msg = '%s %s' %(_debugHeader(parent, traceDepth), msg) - print('%sDEBUG%s: %s' %(bcolors.OKBLUE, bcolors.ENDC, msg), file = sys.stdout) + # Why does this code differ from eg. "error()" + # (where the following lines are NOT part of the "if")? + print('%sDEBUG%s: %s' %(bcolors.OKBLUE, bcolors.ENDC, msg), file=sys.stderr) for line in tools.wrapLine(msg): syslog.syslog(syslog.LOG_DEBUG, 'DEBUG: %s' %line) diff --git a/common/test/test_backintime.py b/common/test/test_backintime.py index a424aa5ff..72751a225 100644 --- a/common/test/test_backintime.py +++ b/common/test/test_backintime.py @@ -125,14 +125,17 @@ def test_local_snapshot_is_successful(self): Back In Time comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions; type `backintime --license' for details. +''', re.MULTILINE)) -INFO: Lock + # The log output completely goes to stderr + self.assertRegex(error.decode(), re.compile(r'''INFO: Lock INFO: Take a new snapshot. Profile: 1 Main profile INFO: Call rsync to take the snapshot INFO: Save config file INFO: Save permissions INFO: Create info file -INFO: Unlock''', re.MULTILINE)) +INFO: Unlock +''', re.MULTILINE)) # get snapshot id subprocess.check_output(["./backintime", @@ -165,8 +168,11 @@ def test_local_snapshot_is_successful(self): This is free software, and you are welcome to redistribute it under certain conditions; type `backintime --license' for details. +''', re.MULTILINE)) -INFO: Restore: /tmp/test/testfile to: /tmp/restored.*''', re.MULTILINE)) + # The log output completely goes to stderr + self.assertRegex(error.decode(), re.compile(r'''INFO: Restore: /tmp/test/testfile to: /tmp/restored.*''', + re.MULTILINE)) # verify that files restored are the same as those backed up subprocess.check_output(["diff",