From 9d19491f52cad798ce995f3f8d1c13e2dc54cb0c Mon Sep 17 00:00:00 2001 From: Marek Blaha Date: Sun, 12 Jan 2020 15:52:16 +0100 Subject: [PATCH 1/2] Split loggers setup so that file loggers could be set separately --- dnf/logging.py | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/dnf/logging.py b/dnf/logging.py index df355efa57..bd660470a3 100644 --- a/dnf/logging.py +++ b/dnf/logging.py @@ -132,12 +132,14 @@ def _paint_mark(logger): class Logging(object): def __init__(self): self.stdout_handler = self.stderr_handler = None - - @only_once - def _presetup(self): logging.addLevelName(DDEBUG, "DDEBUG") logging.addLevelName(SUBDEBUG, "SUBDEBUG") logging.addLevelName(TRACE, "TRACE") + logging.captureWarnings(True) + logging.raiseExceptions = False + + @only_once + def _presetup(self): logger_dnf = logging.getLogger("dnf") logger_dnf.setLevel(TRACE) @@ -155,24 +157,19 @@ def _presetup(self): self.stderr_handler = stderr @only_once - def _setup(self, verbose_level, error_level, logdir, log_size, log_rotate): - self._presetup() + def _setup_file_loggers(self, verbose_level, logdir, log_size, log_rotate): logger_dnf = logging.getLogger("dnf") + logger_dnf.setLevel(TRACE) # setup file logger logfile = os.path.join(logdir, dnf.const.LOG) handler = _create_filehandler(logfile, log_size, log_rotate) logger_dnf.addHandler(handler) - # temporarily turn off stdout/stderr handlers: - self.stdout_handler.setLevel(SUPERCRITICAL) - self.stderr_handler.setLevel(SUPERCRITICAL) # put the marker in the file now: _paint_mark(logger_dnf) # setup Python warnings - logging.captureWarnings(True) logger_warnings = logging.getLogger("py.warnings") - logger_warnings.addHandler(self.stderr_handler) logger_warnings.addHandler(handler) lr_logfile = os.path.join(logdir, dnf.const.LOG_LIBREPO) @@ -184,22 +181,41 @@ def _setup(self, verbose_level, error_level, logdir, log_size, log_rotate): logger_rpm.setLevel(SUBDEBUG) logfile = os.path.join(logdir, dnf.const.LOG_RPM) handler = _create_filehandler(logfile, log_size, log_rotate) - logger_rpm.addHandler(self.stdout_handler) - logger_rpm.addHandler(self.stderr_handler) logger_rpm.addHandler(handler) _paint_mark(logger_rpm) + + @only_once + def _setup(self, verbose_level, error_level, logdir, log_size, log_rotate): + self._presetup() + + # temporarily turn off stdout/stderr handlers: + self.stdout_handler.setLevel(SUPERCRITICAL) + self.stderr_handler.setLevel(SUPERCRITICAL) + + self._setup_file_loggers(verbose_level, logdir, log_size, log_rotate) + + logger_warnings = logging.getLogger("py.warnings") + logger_warnings.addHandler(self.stderr_handler) + + # setup RPM callbacks logger + logger_rpm = logging.getLogger("dnf.rpm") + logger_rpm.addHandler(self.stdout_handler) + logger_rpm.addHandler(self.stderr_handler) + # bring std handlers to the preferred level self.stdout_handler.setLevel(verbose_level) self.stderr_handler.setLevel(error_level) - logging.raiseExceptions = False - def _setup_from_dnf_conf(self, conf): + def _setup_from_dnf_conf(self, conf, file_loggers_only=False): verbose_level_r = _cfg_verbose_val2level(conf.debuglevel) error_level_r = _cfg_err_val2level(conf.errorlevel) logdir = conf.logdir log_size = conf.log_size log_rotate = conf.log_rotate - return self._setup(verbose_level_r, error_level_r, logdir, log_size, log_rotate) + if file_loggers_only: + return self._setup_file_loggers(verbose_level_r, logdir, log_size, log_rotate) + else: + return self._setup(verbose_level_r, error_level_r, logdir, log_size, log_rotate) class Timer(object): From e43eaba4148446523eaf3e8ff1549c7576d00f1c Mon Sep 17 00:00:00 2001 From: Marek Blaha Date: Sun, 12 Jan 2020 15:53:09 +0100 Subject: [PATCH 2/2] New API function base.setup_loggers() (RhBug:1788212) Gives API users ability to setup DNF loggers. https://bugzilla.redhat.com/show_bug.cgi?id=1788212 --- dnf/base.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dnf/base.py b/dnf/base.py index b2c17bba30..56a9dfb478 100644 --- a/dnf/base.py +++ b/dnf/base.py @@ -2536,6 +2536,14 @@ def _raise_package_not_installed_error(self, pkg_spec, forms, reponame): msg = _('All matches were filtered out by exclude filtering for argument') raise dnf.exceptions.PackagesNotInstalledError(msg, pkg_spec) + def setup_loggers(self): + # :api + """ + Setup DNF file loggers based on given configuration file. The loggers are set the same + way as if DNF was run from CLI. + """ + self._logging._setup_from_dnf_conf(self.conf, file_loggers_only=True) + def _msg_installed(pkg): name = ucd(pkg)