aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJake Hunsaker <jhunsake@redhat.com>2023-01-19 13:39:15 -0500
committerJake Hunsaker <jhunsake@redhat.com>2023-01-23 11:15:21 -0500
commit8c9db57ed02c3cafba362b7f2dae301f26b2f419 (patch)
treeeefaee453901f45217abf21bbd3ffb8590754830
parent76c27a82b2688d0fb9a8b6bd5ea9ffd1c896fde9 (diff)
downloadsos-8c9db57ed02c3cafba362b7f2dae301f26b2f419.tar.gz
[Policy] Add logging mechanism to Policy
This commit allows `Policy` to inherit the logging streams used by the rest of sos. With this commit, this is primarily the `ui_log` in order to report info and error level messages to the console. As was discovered earlier, avocado and potentially other automation may sometimes miss `print()` output, and so we should consistently use the logging mechanism. Future commits may be expected to enhace debug logging of Policy functionality. See also: #3106 Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
-rw-r--r--sos/policies/__init__.py56
-rw-r--r--sos/policies/distros/__init__.py7
-rw-r--r--sos/policies/distros/redhat.py20
-rw-r--r--sos/policies/distros/suse.py2
4 files changed, 44 insertions, 41 deletions
diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py
index 40f33ac4..469c2183 100644
--- a/sos/policies/__init__.py
+++ b/sos/policies/__init__.py
@@ -1,3 +1,4 @@
+import logging
import os
import platform
import time
@@ -127,6 +128,8 @@ any third party.
super(SubClass, self).__init__(). Policies that require runtime
tests to construct PATH must call self.set_exec_path() after
modifying PATH in their own initializer."""
+ self.soslog = logging.getLogger('sos')
+ self.ui_log = logging.getLogger('sos_ui')
self._parse_uname()
self.case_id = None
self.probe_runtime = probe_runtime
@@ -429,44 +432,41 @@ any third party.
file for this run
:type map_file: ``str``
"""
- # Logging is already shutdown and all terminal output must use the
- # print() call.
+ # Logging is shut down, but there are some edge cases where automation
+ # does not capture printed output (e.g. avocado CI). Use the ui_log to
+ # still print to console in this case.
# make sure a report exists
if not archive and not directory:
return False
- self._print()
-
if map_file:
- self._print(_("A mapping of obfuscated elements is available at"
- "\n\t%s\n" % map_file))
+ self.ui_log.info(
+ _(f"\nA mapping of obfuscated elements is available at"
+ f"\n\t{map_file}")
+ )
if archive:
- self._print(_("Your sosreport has been generated and saved "
- "in:\n\t%s\n") % archive, always=True)
- self._print(_(" Size\t%s") %
- get_human_readable(archivestat.st_size))
- self._print(_(" Owner\t%s") %
- getpwuid(archivestat.st_uid).pw_name)
+ self.ui_log.info(
+ _(f"\nYour sosreport has been generated and saved in:"
+ f"\n\t{archive}\n")
+ )
+ self.ui_log.info(
+ _(f" Size\t{get_human_readable(archivestat.st_size)}")
+ )
+ self.ui_log.info(
+ _(f" Owner\t{getpwuid(archivestat.st_uid).pw_name}")
+ )
else:
- self._print(_("Your sosreport build tree has been generated "
- "in:\n\t%s\n") % directory, always=True)
+ self.ui_log.info(
+ _(f"Your sosreport build tree has been generated in:"
+ f"\n\t{directory}\n")
+ )
if checksum:
- self._print(" " + self.get_preferred_hash_name() + "\t" + checksum)
- self._print()
- self._print(_("Please send this file to your support "
- "representative."))
- self._print()
-
- def _print(self, msg=None, always=False):
- """A wrapper around print that only prints if we are not running in
- quiet mode"""
- if always or not self.commons['cmdlineopts'].quiet:
- if msg:
- print(msg)
- else:
- print()
+ self.ui_log.info(f" {self.get_preferred_hash_name()}\t{checksum}")
+ self.ui_log.info(
+ _("\nPlease send this file to your support representative.\n")
+ )
def get_msg(self):
"""This method is used to prepare the preamble text to display to
diff --git a/sos/policies/distros/__init__.py b/sos/policies/distros/__init__.py
index a1051fa1..84382c08 100644
--- a/sos/policies/distros/__init__.py
+++ b/sos/policies/distros/__init__.py
@@ -300,9 +300,8 @@ class LinuxPolicy(Policy):
if cmdline_opts.upload and self.get_upload_url():
self.prompt_for_upload_user()
self.prompt_for_upload_password()
- self._print()
+ self.ui_log.info('')
except KeyboardInterrupt:
- self._print()
raise
if cmdline_opts.case_id:
@@ -379,7 +378,9 @@ class LinuxPolicy(Policy):
raise Exception("No upload destination provided by policy or by "
"--upload-url")
upload_func = self._determine_upload_type()
- print(_("Attempting upload to %s" % self.get_upload_url_string()))
+ self.ui_log.info(
+ _(f"Attempting upload to {self.get_upload_url_string()}")
+ )
return upload_func()
def _determine_upload_type(self):
diff --git a/sos/policies/distros/redhat.py b/sos/policies/distros/redhat.py
index 6f1b09ab..92288e74 100644
--- a/sos/policies/distros/redhat.py
+++ b/sos/policies/distros/redhat.py
@@ -330,8 +330,10 @@ support representative.
_user = self.get_upload_user()
_token = json.loads(ret.text)['token']
else:
- print("Unable to retrieve Red Hat auth token using provided "
- "credentials. Will try anonymous.")
+ self.ui_log.error(
+ "Unable to retrieve Red Hat auth token using provided "
+ "credentials. Will try anonymous."
+ )
# we either do not have a username or password/token, or both
if not _token:
adata = {"isAnonymous": True}
@@ -340,11 +342,9 @@ support representative.
resp = json.loads(anon.text)
_user = resp['username']
_token = resp['token']
- print(
- "User '%s'" # lgtm [py/clear-text-logging-sensitive-data]
- "used for anonymous upload. Please inform your support "
- "engineer so they may retrieve the data."
- % _user
+ self.ui_log.info(
+ _(f"User {_user} used for anonymous upload. Please inform "
+ f"your support engineer so they may retrieve the data.")
)
if _user and _token:
return super(RHELPolicy, self).upload_sftp(user=_user,
@@ -365,8 +365,10 @@ support representative.
if not self.upload_url.startswith(RH_API_HOST):
raise
else:
- print("Upload to Red Hat Customer Portal failed. Trying %s"
- % RH_SFTP_HOST)
+ self.ui_log.error(
+ _(f"Upload to Red Hat Customer Portal failed. Trying "
+ f"{RH_SFTP_HOST}")
+ )
self.upload_url = RH_SFTP_HOST
uploaded = super(RHELPolicy, self).upload_archive(archive)
return uploaded
diff --git a/sos/policies/distros/suse.py b/sos/policies/distros/suse.py
index 7fa0b0c9..9422daf8 100644
--- a/sos/policies/distros/suse.py
+++ b/sos/policies/distros/suse.py
@@ -34,7 +34,7 @@ class SuSEPolicy(LinuxPolicy):
# If rpm query timed out after timeout duration exit
if not self.package_manager.packages:
- print("Could not obtain installed package list", file=sys.stderr)
+ self.ui_log.error("Could not obtain installed package list.")
sys.exit(1)
self.PATH = "/usr/sbin:/usr/bin:/root/bin:/sbin"