From 78ad2336538ec7d747491e9ca169161c0d5f8100 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Wed, 11 Dec 2019 17:34:03 -0500 Subject: [PATCH] tests: abide logging options for clitests We need to restore logging after calling the cli tools. Centralize the logging reset behavior since we need that too Signed-off-by: Cole Robinson --- tests/__init__.py | 6 ++---- tests/clitest.py | 3 +++ virtinst/__init__.py | 2 +- virtinst/cli.py | 12 +++--------- virtinst/logger.py | 12 ++++++++++++ 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 609d7d8c4..163969680 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -14,7 +14,7 @@ os.environ.pop("_ARC_DEBUG", None) # pylint: disable=wrong-import-position from virtinst import buildconfig -from virtinst import log +from virtinst import log, reset_logging # This sets all the cli bits back to their defaults imp.reload(buildconfig) @@ -28,9 +28,7 @@ virtxml = None def setup_logging(): import logging - rootLogger = logging.getLogger() - for handler in rootLogger.handlers: - rootLogger.removeHandler(handler) + reset_logging() fmt = "%(levelname)-8s %(message)s" streamHandler = logging.StreamHandler() diff --git a/tests/clitest.py b/tests/clitest.py index 7f584f293..beef8fd7a 100644 --- a/tests/clitest.py +++ b/tests/clitest.py @@ -21,6 +21,7 @@ from virtinst import log from virtinst import OSDB from virtinst.install import unattended +from tests import setup_logging from tests import virtinstall, virtclone, virtconvert, virtxml from tests import utils @@ -244,6 +245,8 @@ class Command(object): sys.argv = oldargv if oldenv: os.environ = oldenv + # Reset logging + setup_logging() def _get_output(self, conn): diff --git a/virtinst/__init__.py b/virtinst/__init__.py index d7f5ec552..7d0972205 100644 --- a/virtinst/__init__.py +++ b/virtinst/__init__.py @@ -65,4 +65,4 @@ from virtinst.snapshot import DomainSnapshot from virtinst.connection import VirtinstConnection -from virtinst.logger import log +from virtinst.logger import log, reset_logging diff --git a/virtinst/cli.py b/virtinst/cli.py index 1e0a001db..1fbafae24 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -23,7 +23,7 @@ from .buildconfig import BuildConfig from .connection import VirtinstConnection from .devices import (Device, DeviceController, DeviceDisk, DeviceGraphics, DeviceInterface, DevicePanic) -from .logger import log +from .logger import log, reset_logging from .nodedev import NodeDevice from .osdict import OSDB from .storage import StoragePool, StorageVolume @@ -119,6 +119,7 @@ def setupParser(usage, description, introspection_epilog=False): def earlyLogging(): + reset_logging() import logging logging.basicConfig(level=logging.DEBUG, format='%(message)s') @@ -159,14 +160,7 @@ def setupLogging(appname, debug_stdout, do_quiet, cli_app=True): import logging import logging.handlers - rootLogger = logging.getLogger() - - # Undo early logging - for handler in rootLogger.handlers: - rootLogger.removeHandler(handler) - # Undo any logging on our log handler. Needed for test suite - for handler in log.handlers: - log.removeHandler(handler) + reset_logging() log.setLevel(logging.DEBUG) if logfile: diff --git a/virtinst/logger.py b/virtinst/logger.py index 07244c0dd..f529f7d72 100644 --- a/virtinst/logger.py +++ b/virtinst/logger.py @@ -7,3 +7,15 @@ import logging # This is exported by virtinst/__init__.py log = logging.getLogger("virtinst") + + +def reset_logging(): + rootLogger = logging.getLogger() + + # Undo early logging + for handler in rootLogger.handlers: + rootLogger.removeHandler(handler) + + # Undo any logging on our log handler. Needed for test suite + for handler in log.handlers: + log.removeHandler(handler)