From f8a5bc94e5a63b89e8c3f4062d288769b3717484 Mon Sep 17 00:00:00 2001 From: Miroslav Kovar Date: Thu, 17 Oct 2019 20:55:58 +0200 Subject: [PATCH] Pass root logger instead --- lbry/lbry/extras/cli.py | 30 +++++++++++++------------- lbry/tests/unit/test_cli.py | 43 +++++++++++++++---------------------- 2 files changed, 32 insertions(+), 41 deletions(-) diff --git a/lbry/lbry/extras/cli.py b/lbry/lbry/extras/cli.py index eda92bf7c..3d3e3f436 100644 --- a/lbry/lbry/extras/cli.py +++ b/lbry/lbry/extras/cli.py @@ -8,7 +8,6 @@ import argparse import logging import logging.handlers from docopt import docopt -from types import ModuleType import aiohttp from aiohttp.web import GracefulExit @@ -222,44 +221,45 @@ def ensure_directory_exists(path: str): def setup_logging(args: argparse.Namespace, conf: Config, loop: asyncio.AbstractEventLoop, - logging: ModuleType = logging): + root_logger: logging.Logger): default_formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s") file_handler = logging.handlers.RotatingFileHandler( conf.log_file_path, maxBytes=2097152, backupCount=5 ) file_handler.setFormatter(default_formatter) - logging.getLogger('lbry').addHandler(file_handler) - logging.getLogger('lbry').addHandler(logging.NullHandler()) - logging.getLogger('torba').addHandler(file_handler) + root_logger.getChild('lbry').addHandler(file_handler) + root_logger.getChild('lbry').addHandler(logging.NullHandler()) + root_logger.getChild('torba').addHandler(file_handler) if not args.quiet: handler = logging.StreamHandler() handler.setFormatter(default_formatter) - logging.getLogger('lbry').addHandler(handler) - logging.getLogger('torba').addHandler(handler) - logging.getLogger('torba').setLevel(logging.INFO) + root_logger.getChild('lbry').addHandler(handler) + root_logger.getChild('torba').addHandler(handler) + root_logger.getChild('torba').setLevel(logging.INFO) - logging.getLogger('aioupnp').setLevel(logging.WARNING) - logging.getLogger('aiohttp').setLevel(logging.CRITICAL) + root_logger.getChild('aioupnp').setLevel(logging.WARNING) + root_logger.getChild('aiohttp').setLevel(logging.CRITICAL) - logging.getLogger('lbry').setLevel(logging.INFO) + root_logger.getChild('lbry').setLevel(logging.INFO) if args.verbose is not None: loop.set_debug(True) if len(args.verbose) > 0: for module in args.verbose: - logging.getLogger(module).setLevel(logging.DEBUG) + root_logger.getChild(module).setLevel(logging.DEBUG) else: - logging.getLogger('lbry').setLevel(logging.DEBUG) + root_logger.getChild('lbry').setLevel(logging.DEBUG) if conf.share_usage_data: loggly_handler = get_loggly_handler() loggly_handler.setLevel(logging.ERROR) - logging.getLogger('lbry').addHandler(loggly_handler) + root_logger.getChild('lbry').addHandler(loggly_handler) def run_daemon(args: argparse.Namespace, conf: Config): loop = asyncio.get_event_loop() - setup_logging(args, conf, loop) + root_logger = logging.getLogger() + setup_logging(args, conf, loop, root_logger) daemon = Daemon(conf) def __exit(): diff --git a/lbry/tests/unit/test_cli.py b/lbry/tests/unit/test_cli.py index 51e465122..e7e58111a 100644 --- a/lbry/tests/unit/test_cli.py +++ b/lbry/tests/unit/test_cli.py @@ -1,6 +1,7 @@ import contextlib import asyncio import importlib +import logging from io import StringIO from unittest import TestCase @@ -14,21 +15,7 @@ from lbry.conf import Config from lbry.extras import cli -class CLILoggingTest(AsyncioTestCase): - - async def asyncSetUp(self): - def import_module(name): - spec = importlib.util.find_spec(name) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - return module - - self.test_loop = asyncio.new_event_loop() - asyncio.set_event_loop(self.test_loop) - - self.logging = import_module('logging') - handlers = import_module('logging.handlers') - self.logging.handlers = handlers +class CLILoggingTest(TestCase): def test_setup_logging(self): def setup(argv): @@ -36,23 +23,27 @@ class CLILoggingTest(AsyncioTestCase): args, command_args = parser.parse_known_args(argv) conf = Config.create_from_arguments(args) conf.data_dir = '/tmp' - setup_logging(args, conf, self.test_loop, self.logging) + setup_logging(args, conf, test_loop, root_logger) + + test_loop = asyncio.new_event_loop() + asyncio.set_event_loop(test_loop) + root_logger = logging.getLogger('test_logger') setup(["start"]) - self.assertTrue(self.logging.getLogger("lbry").isEnabledFor(self.logging.INFO)) - self.assertFalse(self.logging.getLogger("lbry").isEnabledFor(self.logging.DEBUG)) + self.assertTrue(root_logger.getChild("lbry").isEnabledFor(logging.INFO)) + self.assertFalse(root_logger.getChild("lbry").isEnabledFor(logging.DEBUG)) setup(["start", "--verbose"]) - self.assertTrue(self.logging.getLogger("lbry").isEnabledFor(self.logging.DEBUG)) - self.assertTrue(self.logging.getLogger("lbry").isEnabledFor(self.logging.INFO)) - self.assertFalse(self.logging.getLogger("torba").isEnabledFor(self.logging.DEBUG)) + self.assertTrue(root_logger.getChild("lbry").isEnabledFor(logging.DEBUG)) + self.assertTrue(root_logger.getChild("lbry").isEnabledFor(logging.INFO)) + self.assertFalse(root_logger.getChild("torba").isEnabledFor(logging.DEBUG)) setup(["start", "--verbose", "lbry.extras", "lbry.wallet", "torba.client"]) - self.assertTrue(self.logging.getLogger("lbry.extras").isEnabledFor(self.logging.DEBUG)) - self.assertTrue(self.logging.getLogger("lbry.wallet").isEnabledFor(self.logging.DEBUG)) - self.assertTrue(self.logging.getLogger("torba.client").isEnabledFor(self.logging.DEBUG)) - self.assertFalse(self.logging.getLogger("lbry").isEnabledFor(self.logging.DEBUG)) - self.assertFalse(self.logging.getLogger("torba").isEnabledFor(self.logging.DEBUG)) + self.assertTrue(root_logger.getChild("lbry.extras").isEnabledFor(logging.DEBUG)) + self.assertTrue(root_logger.getChild("lbry.wallet").isEnabledFor(logging.DEBUG)) + self.assertTrue(root_logger.getChild("torba.client").isEnabledFor(logging.DEBUG)) + self.assertFalse(root_logger.getChild("lbry").isEnabledFor(logging.DEBUG)) + self.assertFalse(root_logger.getChild("torba").isEnabledFor(logging.DEBUG)) class CLITest(AsyncioTestCase):