Make better use of vlogging.

This commit is contained in:
voussoir 2021-09-08 19:31:56 -07:00
parent e17b6668f3
commit ad306ae771
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
3 changed files with 17 additions and 22 deletions

View file

@ -8,6 +8,9 @@ import time
from voussoirkit import flasktools from voussoirkit import flasktools
from voussoirkit import pathclass from voussoirkit import pathclass
from voussoirkit import vlogging
log = vlogging.getLogger(__name__)
import ycdl import ycdl
@ -61,7 +64,7 @@ def init_ycdldb(*args, **kwargs):
def refresher_thread(rate): def refresher_thread(rate):
while True: while True:
time.sleep(rate) time.sleep(rate)
print('Starting refresh job.') log.info('Starting refresh job.')
thread_kwargs = {'force': False, 'skip_failures': True} thread_kwargs = {'force': False, 'skip_failures': True}
refresh_job = threading.Thread( refresh_job = threading.Thread(
target=ycdldb.refresh_all_channels, target=ycdldb.refresh_all_channels,
@ -71,6 +74,6 @@ def refresher_thread(rate):
refresh_job.start() refresh_job.start()
def start_refresher_thread(rate): 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 = threading.Thread(target=refresher_thread, args=[rate], daemon=True)
refresher.start() refresher.start()

View file

@ -6,12 +6,6 @@ python ycdl_flask_dev.py [port]
''' '''
import gevent.monkey; gevent.monkey.patch_all() 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 argparse
import gevent.pywsgi import gevent.pywsgi
import os import os
@ -21,6 +15,8 @@ from voussoirkit import operatornotify
from voussoirkit import pathclass from voussoirkit import pathclass
from voussoirkit import vlogging from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'ycdl_flask_dev')
import ycdl import ycdl
import youtube_credentials import youtube_credentials
import backend import backend
@ -33,7 +29,6 @@ site.debug = True
site = backend.site site = backend.site
HTTPS_DIR = pathclass.Path(__file__).parent.with_child('https') HTTPS_DIR = pathclass.Path(__file__).parent.with_child('https')
LOG_LEVEL = vlogging.NOTSET
def ycdl_flask_launch( def ycdl_flask_launch(
*, *,
@ -63,21 +58,23 @@ def ycdl_flask_launch(
site.localhost_only = True site.localhost_only = True
youtube_core = ycdl.ytapi.Youtube(youtube_credentials.get_youtube_key()) youtube_core = ycdl.ytapi.Youtube(youtube_credentials.get_youtube_key())
backend.common.init_ycdldb(youtube_core, create=create, log_level=LOG_LEVEL) backend.common.init_ycdldb(youtube_core, create=create)
ycdl.ytrss.log.setLevel(LOG_LEVEL)
if refresh_rate is not None:
backend.common.start_refresher_thread(refresh_rate)
message = f'Starting server on port {port}, pid={os.getpid()}.' message = f'Starting server on port {port}, pid={os.getpid()}.'
if use_https: if use_https:
message += ' (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: try:
http.serve_forever() http.serve_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
pass log.info('Goodbye')
return 0
def ycdl_flask_launch_argparse(args): def ycdl_flask_launch_argparse(args):
return ycdl_flask_launch( return ycdl_flask_launch(
@ -88,11 +85,9 @@ def ycdl_flask_launch_argparse(args):
use_https=args.use_https, use_https=args.use_https,
) )
@vlogging.main_decorator
@operatornotify.main_decorator(subject='YCDL', notify_every_line=True) @operatornotify.main_decorator(subject='YCDL', notify_every_line=True)
def main(argv): def main(argv):
global LOG_LEVEL
(LOG_LEVEL, argv) = vlogging.get_level_by_argv(argv)
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('port', nargs='?', type=int, default=5000) parser.add_argument('port', nargs='?', type=int, default=5000)

View file

@ -526,7 +526,6 @@ class YCDLDB(
youtube, youtube,
create=True, create=True,
data_directory=None, data_directory=None,
log_level=vlogging.NOTSET,
skip_version_check=False, skip_version_check=False,
): ):
super().__init__() super().__init__()
@ -543,8 +542,6 @@ class YCDLDB(
# LOGGING # LOGGING
self.log = vlogging.getLogger(f'{__name__}:{self.data_directory.absolute_path}') self.log = vlogging.getLogger(f'{__name__}:{self.data_directory.absolute_path}')
self.log.setLevel(log_level)
self.youtube.log.setLevel(log_level)
# DATABASE # DATABASE
self.database_filepath = self.data_directory.with_child(constants.DEFAULT_DBNAME) self.database_filepath = self.data_directory.with_child(constants.DEFAULT_DBNAME)