From 0322fa567b0b86653f8fdfb937dd16e1d435e460 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Wed, 14 Dec 2022 05:31:45 -0500 Subject: [PATCH] set log level from database config --- relay/application.py | 4 ++++ relay/database.py | 4 ++++ relay/logger.py | 28 +++++++++++++++------------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/relay/application.py b/relay/application.py index 361c0cc..16325fd 100644 --- a/relay/application.py +++ b/relay/application.py @@ -14,6 +14,7 @@ from datetime import datetime, timedelta from .config import Config from .database import Database from .http_client import HttpClient +from .logger import set_level from .misc import DotDict, check_open_port, set_app from .views import routes @@ -41,6 +42,9 @@ class Application(web.Application): self.database.create() self.set_signal_handler() + with self.database.session as s: + set_level(s.get_config('log_level')) + @property def client(self): diff --git a/relay/database.py b/relay/database.py index 1759f6b..f595d62 100644 --- a/relay/database.py +++ b/relay/database.py @@ -4,6 +4,7 @@ from datetime import datetime from tinysql import Column, Table from urllib.parse import urlparse +from .logger import set_level from .misc import AppBase, DotDict, boolean @@ -245,6 +246,9 @@ class Connection(tinysql.ConnectionMixin): if value == '__DEFAULT__': value = DEFAULT_CONFIG[key][1] + if key == 'log_level': + set_level(value) + row = self.select('config', key=key).one() if row: diff --git a/relay/logger.py b/relay/logger.py index 166cbf2..e25efb0 100644 --- a/relay/logger.py +++ b/relay/logger.py @@ -4,6 +4,16 @@ import os from pathlib import Path +LEVELS = { + 'critical': logging.CRITICAL, + 'error': logging.ERROR, + 'warning': logging.WARNING, + 'info': logging.INFO, + 'verbose': 15, + 'debug': logging.DEBUG +} + + ## Add the verbose logging level def verbose(message, *args, **kwargs): if not logging.root.isEnabledFor(logging.VERBOSE): @@ -15,10 +25,6 @@ setattr(logging, 'verbose', verbose) setattr(logging, 'VERBOSE', 15) logging.addLevelName(15, 'VERBOSE') - -## Get log level and file from environment if possible -env_log_level = os.environ.get('LOG_LEVEL', 'INFO').upper() - try: env_log_file = Path(os.environ.get('LOG_FILE')).expanduser().resolve() @@ -26,14 +32,6 @@ except TypeError: env_log_file = None -## Make sure the level from the environment is valid -try: - log_level = getattr(logging, env_log_level) - -except AttributeError: - log_level = logging.INFO - - ## Set logging config handlers = [logging.StreamHandler()] @@ -41,7 +39,11 @@ if env_log_file: handlers.append(logging.FileHandler(env_log_file)) logging.basicConfig( - level = log_level, + level = logging.INFO, format = "[%(asctime)s] %(levelname)s: %(message)s", handlers = handlers ) + + +def set_level(level): + logging.getLogger().setLevel(LEVELS[level.lower()])