minor fixups for logging

This commit is contained in:
Lex Berezhny 2019-10-18 10:23:35 -04:00
parent f8a5bc94e5
commit 4e7e475a5e
2 changed files with 34 additions and 36 deletions

View file

@ -220,46 +220,46 @@ def ensure_directory_exists(path: str):
pathlib.Path(path).mkdir(parents=True, exist_ok=True) pathlib.Path(path).mkdir(parents=True, exist_ok=True)
def setup_logging(args: argparse.Namespace, conf: Config, loop: asyncio.AbstractEventLoop, LOG_MODULES = ('lbry', 'torba', 'aioupnp')
root_logger: logging.Logger):
def setup_logging(args: argparse.Namespace, conf: Config, logger: logging.Logger):
default_formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s") default_formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s")
file_handler = logging.handlers.RotatingFileHandler( file_handler = logging.handlers.RotatingFileHandler(
conf.log_file_path, maxBytes=2097152, backupCount=5 conf.log_file_path, maxBytes=2097152, backupCount=5
) )
file_handler.setFormatter(default_formatter) file_handler.setFormatter(default_formatter)
root_logger.getChild('lbry').addHandler(file_handler) for module_name in LOG_MODULES:
root_logger.getChild('lbry').addHandler(logging.NullHandler()) logger.getChild(module_name).addHandler(file_handler)
root_logger.getChild('torba').addHandler(file_handler)
if not args.quiet: if not args.quiet:
handler = logging.StreamHandler() handler = logging.StreamHandler()
handler.setFormatter(default_formatter) handler.setFormatter(default_formatter)
root_logger.getChild('lbry').addHandler(handler) for module_name in LOG_MODULES:
root_logger.getChild('torba').addHandler(handler) logger.getChild(module_name).addHandler(handler)
root_logger.getChild('torba').setLevel(logging.INFO)
root_logger.getChild('aioupnp').setLevel(logging.WARNING) logger.getChild('lbry').setLevel(logging.INFO)
root_logger.getChild('aiohttp').setLevel(logging.CRITICAL) logger.getChild('torba').setLevel(logging.INFO)
logger.getChild('aioupnp').setLevel(logging.WARNING)
logger.getChild('aiohttp').setLevel(logging.CRITICAL)
root_logger.getChild('lbry').setLevel(logging.INFO)
if args.verbose is not None: if args.verbose is not None:
loop.set_debug(True)
if len(args.verbose) > 0: if len(args.verbose) > 0:
for module in args.verbose: for module in args.verbose:
root_logger.getChild(module).setLevel(logging.DEBUG) logger.getChild(module).setLevel(logging.DEBUG)
else: else:
root_logger.getChild('lbry').setLevel(logging.DEBUG) logger.getChild('lbry').setLevel(logging.DEBUG)
if conf.share_usage_data: if conf.share_usage_data:
loggly_handler = get_loggly_handler() loggly_handler = get_loggly_handler()
loggly_handler.setLevel(logging.ERROR) loggly_handler.setLevel(logging.ERROR)
root_logger.getChild('lbry').addHandler(loggly_handler) logger.getChild('lbry').addHandler(loggly_handler)
def run_daemon(args: argparse.Namespace, conf: Config): def run_daemon(args: argparse.Namespace, conf: Config):
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
root_logger = logging.getLogger() if args.verbose is not None:
setup_logging(args, conf, loop, root_logger) loop.set_debug(True)
setup_logging(args, conf, logging.getLogger())
daemon = Daemon(conf) daemon = Daemon(conf)
def __exit(): def __exit():

View file

@ -1,6 +1,4 @@
import contextlib import contextlib
import asyncio
import importlib
import logging import logging
from io import StringIO from io import StringIO
from unittest import TestCase from unittest import TestCase
@ -15,35 +13,35 @@ from lbry.conf import Config
from lbry.extras import cli from lbry.extras import cli
class CLILoggingTest(TestCase): class CLILoggingTest(AsyncioTestCase):
async def test_setup_logging(self):
# test needs to be async to avoid warnings from loggly
def test_setup_logging(self):
def setup(argv): def setup(argv):
parser = cli.get_argument_parser() parser = cli.get_argument_parser()
args, command_args = parser.parse_known_args(argv) args, command_args = parser.parse_known_args(argv)
conf = Config.create_from_arguments(args) conf = Config.create_from_arguments(args)
conf.data_dir = '/tmp' conf.data_dir = '/tmp'
setup_logging(args, conf, test_loop, root_logger) setup_logging(args, conf, logger)
test_loop = asyncio.new_event_loop() logger = logging.getLogger('test_logger')
asyncio.set_event_loop(test_loop)
root_logger = logging.getLogger('test_logger')
setup(["start"]) setup(["start"])
self.assertTrue(root_logger.getChild("lbry").isEnabledFor(logging.INFO)) self.assertTrue(logger.getChild("lbry").isEnabledFor(logging.INFO))
self.assertFalse(root_logger.getChild("lbry").isEnabledFor(logging.DEBUG)) self.assertFalse(logger.getChild("lbry").isEnabledFor(logging.DEBUG))
setup(["start", "--verbose"]) setup(["start", "--verbose"])
self.assertTrue(root_logger.getChild("lbry").isEnabledFor(logging.DEBUG)) self.assertTrue(logger.getChild("lbry").isEnabledFor(logging.DEBUG))
self.assertTrue(root_logger.getChild("lbry").isEnabledFor(logging.INFO)) self.assertTrue(logger.getChild("lbry").isEnabledFor(logging.INFO))
self.assertFalse(root_logger.getChild("torba").isEnabledFor(logging.DEBUG)) self.assertFalse(logger.getChild("torba").isEnabledFor(logging.DEBUG))
setup(["start", "--verbose", "lbry.extras", "lbry.wallet", "torba.client"]) setup(["start", "--verbose", "lbry.extras", "lbry.wallet", "torba.client"])
self.assertTrue(root_logger.getChild("lbry.extras").isEnabledFor(logging.DEBUG)) self.assertTrue(logger.getChild("lbry.extras").isEnabledFor(logging.DEBUG))
self.assertTrue(root_logger.getChild("lbry.wallet").isEnabledFor(logging.DEBUG)) self.assertTrue(logger.getChild("lbry.wallet").isEnabledFor(logging.DEBUG))
self.assertTrue(root_logger.getChild("torba.client").isEnabledFor(logging.DEBUG)) self.assertTrue(logger.getChild("torba.client").isEnabledFor(logging.DEBUG))
self.assertFalse(root_logger.getChild("lbry").isEnabledFor(logging.DEBUG)) self.assertFalse(logger.getChild("lbry").isEnabledFor(logging.DEBUG))
self.assertFalse(root_logger.getChild("torba").isEnabledFor(logging.DEBUG)) self.assertFalse(logger.getChild("torba").isEnabledFor(logging.DEBUG))
class CLITest(AsyncioTestCase): class CLITest(AsyncioTestCase):