From ad306ae771e40745687f697723a57c891850fe1d Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 8 Sep 2021 19:31:56 -0700 Subject: [PATCH] Make better use of vlogging. --- frontends/ycdl_flask/backend/common.py | 7 +++++-- frontends/ycdl_flask/ycdl_flask_dev.py | 29 +++++++++++--------------- ycdl/ycdldb.py | 3 --- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/frontends/ycdl_flask/backend/common.py b/frontends/ycdl_flask/backend/common.py index ae27c39..9fe9d0d 100644 --- a/frontends/ycdl_flask/backend/common.py +++ b/frontends/ycdl_flask/backend/common.py @@ -8,6 +8,9 @@ import time from voussoirkit import flasktools from voussoirkit import pathclass +from voussoirkit import vlogging + +log = vlogging.getLogger(__name__) import ycdl @@ -61,7 +64,7 @@ def init_ycdldb(*args, **kwargs): def refresher_thread(rate): while True: time.sleep(rate) - print('Starting refresh job.') + log.info('Starting refresh job.') thread_kwargs = {'force': False, 'skip_failures': True} refresh_job = threading.Thread( target=ycdldb.refresh_all_channels, @@ -71,6 +74,6 @@ def refresher_thread(rate): refresh_job.start() def start_refresher_thread(rate): - print(f'Starting refresher thread, once per {rate} seconds.') + log.info('Starting refresher thread, once per %d seconds.', rate) refresher = threading.Thread(target=refresher_thread, args=[rate], daemon=True) refresher.start() diff --git a/frontends/ycdl_flask/ycdl_flask_dev.py b/frontends/ycdl_flask/ycdl_flask_dev.py index d9e4c8d..69a7f9e 100644 --- a/frontends/ycdl_flask/ycdl_flask_dev.py +++ b/frontends/ycdl_flask/ycdl_flask_dev.py @@ -6,12 +6,6 @@ python ycdl_flask_dev.py [port] ''' import gevent.monkey; gevent.monkey.patch_all() -import logging -handler = logging.StreamHandler() -log_format = '{levelname}:ycdl.{module}.{funcName}: {message}' -handler.setFormatter(logging.Formatter(log_format, style='{')) -logging.getLogger().addHandler(handler) - import argparse import gevent.pywsgi import os @@ -21,6 +15,8 @@ from voussoirkit import operatornotify from voussoirkit import pathclass from voussoirkit import vlogging +log = vlogging.getLogger(__name__, 'ycdl_flask_dev') + import ycdl import youtube_credentials import backend @@ -33,7 +29,6 @@ site.debug = True site = backend.site HTTPS_DIR = pathclass.Path(__file__).parent.with_child('https') -LOG_LEVEL = vlogging.NOTSET def ycdl_flask_launch( *, @@ -63,21 +58,23 @@ def ycdl_flask_launch( site.localhost_only = True youtube_core = ycdl.ytapi.Youtube(youtube_credentials.get_youtube_key()) - backend.common.init_ycdldb(youtube_core, create=create, log_level=LOG_LEVEL) - ycdl.ytrss.log.setLevel(LOG_LEVEL) - - if refresh_rate is not None: - backend.common.start_refresher_thread(refresh_rate) + backend.common.init_ycdldb(youtube_core, create=create) message = f'Starting server on port {port}, pid={os.getpid()}.' if use_https: message += ' (https)' - print(message) + log.info(message) + + if refresh_rate is None: + log.info('No background refresher thread because --refresh-rate was not passed.') + else: + backend.common.start_refresher_thread(refresh_rate) try: http.serve_forever() except KeyboardInterrupt: - pass + log.info('Goodbye') + return 0 def ycdl_flask_launch_argparse(args): return ycdl_flask_launch( @@ -88,11 +85,9 @@ def ycdl_flask_launch_argparse(args): use_https=args.use_https, ) +@vlogging.main_decorator @operatornotify.main_decorator(subject='YCDL', notify_every_line=True) def main(argv): - global LOG_LEVEL - (LOG_LEVEL, argv) = vlogging.get_level_by_argv(argv) - parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('port', nargs='?', type=int, default=5000) diff --git a/ycdl/ycdldb.py b/ycdl/ycdldb.py index ee50497..d560657 100644 --- a/ycdl/ycdldb.py +++ b/ycdl/ycdldb.py @@ -526,7 +526,6 @@ class YCDLDB( youtube, create=True, data_directory=None, - log_level=vlogging.NOTSET, skip_version_check=False, ): super().__init__() @@ -543,8 +542,6 @@ class YCDLDB( # LOGGING self.log = vlogging.getLogger(f'{__name__}:{self.data_directory.absolute_path}') - self.log.setLevel(log_level) - self.youtube.log.setLevel(log_level) # DATABASE self.database_filepath = self.data_directory.with_child(constants.DEFAULT_DBNAME)