diff --git a/api/api.py b/api/api.py index 56c28fc..8d67111 100644 --- a/api/api.py +++ b/api/api.py @@ -21,6 +21,7 @@ # see https://fastapi.tiangolo.com/deployment/docker/#replication-number-of-processes for comment on worker counts + # logger = tooling.logger_init('DEBUG') logger = tooling.logger_init() # check env and use defaults if not present @@ -28,7 +29,7 @@ env = tooling.check_env_vars() # set logger level based on what we got back - + tooling.set_log_level(env['LOG_LEVEL']) # dump environment we care about diff --git a/api/routers/configuration/delete.py b/api/routers/configuration/delete.py index 88dc577..05fdb6b 100644 --- a/api/routers/configuration/delete.py +++ b/api/routers/configuration/delete.py @@ -1,7 +1,7 @@ ## Delete Configuration operation import json -# from ...tooling.logger import logger +import logging from fastapi import Request, Response, status, Query from ..database.redis import check_conf_server @@ -15,6 +15,8 @@ async def delete_config( config_section: str = Query(None, alias='section') ): + logger = logging.getLogger('api') + route_path = request.url.path if config_op == "delete" and instance_param and redis_param: diff --git a/api/routers/configuration/get.py b/api/routers/configuration/get.py index 0153c00..0f55dad 100644 --- a/api/routers/configuration/get.py +++ b/api/routers/configuration/get.py @@ -1,6 +1,6 @@ # Get Configuration operation -# from ...tooling import logger +import logging from fastapi import Request, Response, status, Query from ..database.redis import check_conf_server @@ -13,6 +13,8 @@ async def get_config( config_section: str | None = None ): + logger = logging.getLogger('api') + route_path = request.url.path logger.debug(f'route_path: ${route_path}') diff --git a/api/routers/configuration/put.py b/api/routers/configuration/put.py index 8a73eae..70d27b5 100644 --- a/api/routers/configuration/put.py +++ b/api/routers/configuration/put.py @@ -1,7 +1,7 @@ # Put Configuration operation import json -# from ...tooling.logger_init import logger +import logging from fastapi import Request, Response, status, Query from ..database.redis import check_conf_server @@ -14,6 +14,8 @@ async def put_config( config_section: str = Query(None, alias='section') ): + logger = logging.getLogger('api') + route_path = request.url.path diff --git a/api/routers/configuration/update.py b/api/routers/configuration/update.py index 4cb5df5..9a38785 100644 --- a/api/routers/configuration/update.py +++ b/api/routers/configuration/update.py @@ -1,7 +1,7 @@ # Update Configuration operation import json -from api.tooling.logger import logger +import logging from fastapi import Request, Response, status, Query from api.routers.database.redis import check_conf_server diff --git a/api/routers/database/couchbase.py b/api/routers/database/couchbase.py index f005cbf..ea05b95 100644 --- a/api/routers/database/couchbase.py +++ b/api/routers/database/couchbase.py @@ -1,7 +1,10 @@ from couchbase.auth import PasswordAuthenticator from couchbase.cluster import Cluster, ClusterOptions from couchbase.exceptions import CouchbaseException, DocumentNotFoundException, TimeoutException -# from api.tooling.logger import logger + +import logging + + def check_couchbase(couchbase_param): """ @@ -12,6 +15,8 @@ def check_couchbase(couchbase_param): :return: (status_code, couchbase) tuple where status_code is boolean if connection fails, and bucket is the Couchbase bucket if successful """ + logger = logging.getLogger('api') + # did we get all required params? if ( not couchbase_param.endpoint or not couchbase_param.username or diff --git a/api/routers/database/operations.py b/api/routers/database/operations.py index b4466f0..420c3f7 100644 --- a/api/routers/database/operations.py +++ b/api/routers/database/operations.py @@ -1,10 +1,12 @@ # Database operations -# from ...tooling.logger import logger +import logging from fastapi import APIRouter def init_router() -> APIRouter: + logger = logging.getLogger('api') + router = APIRouter() return router diff --git a/api/routers/database/redis.py b/api/routers/database/redis.py index 5d37807..941eb8d 100644 --- a/api/routers/database/redis.py +++ b/api/routers/database/redis.py @@ -1,6 +1,8 @@ import redis import urllib.parse -# from ...tooling.logger import logger +import logging + + def check_conf_server(host_param): """ @@ -11,6 +13,8 @@ def check_conf_server(host_param): :return: (status_code, r) tuple where status_code is boolean for the check success, and r is the Redis handle if successful """ + logger = logging.getLogger('api') + redissplit = urllib.parse.urlsplit('//' + host_param) # split parameter into .hostname and .port if redissplit.port: redisport = redissplit.port diff --git a/api/routers/internal/get_config_section.py b/api/routers/internal/get_config_section.py index 9516ab8..312b33d 100644 --- a/api/routers/internal/get_config_section.py +++ b/api/routers/internal/get_config_section.py @@ -1,7 +1,10 @@ import requests +import logging def get_config_section(api_url, redis_url, instance, section): + logger = logging.getLogger('api') + params = { } diff --git a/api/routers/internal/toml.py b/api/routers/internal/toml.py index 446fa4b..7a1eb36 100644 --- a/api/routers/internal/toml.py +++ b/api/routers/internal/toml.py @@ -8,11 +8,13 @@ import sys import json - +import logging def toml_import(logger, toml_file) : + logger = logging.getLogger('api') + toml_dict = None logger.debug("> " + sys._getframe().f_code.co_name + "()") @@ -29,6 +31,8 @@ def toml_import(logger, toml_file) : def toml_export(logger, redis, instance_name, table_name) : + logger = logging.getLogger('api') + logger.debug("> " + sys._getframe().f_code.co_name + "()") # initialize toml_dict dict since it doesn't exist here diff --git a/api/tooling/check_env_vars.py b/api/tooling/check_env_vars.py index b48734c..4a8830f 100644 --- a/api/tooling/check_env_vars.py +++ b/api/tooling/check_env_vars.py @@ -13,7 +13,7 @@ def check_env_vars() -> Dict: """ # access the 'global' logger - logger = logging.getLogger(__name__) + logger = logging.getLogger('api') vars = [ 'REDIS_HOST', diff --git a/api/tooling/logger_init.py b/api/tooling/logger_init.py index 342b9a7..5074d85 100644 --- a/api/tooling/logger_init.py +++ b/api/tooling/logger_init.py @@ -29,18 +29,19 @@ class ANSIColors: yellow = '\033[33m' red = '\033[31m' bold_red = '\033[31;1m' - cyan = '\036m' + cyan = '\033[36m' reset = '\033[0m' # Define custom formats for each severity level level_formats = { - logging.DEBUG: ANSIColors.green + '%(levelname)s:' + ANSIColors.white + '\t %(asctime)s %(module)s' + ANSIColors.green + ' %(message)s' + ANSIColors.reset, - logging.INFO: ANSIColors.cyan + '%(levelname)s:' + ANSIColors.reset + '\t %(message)s', + # logging.DEBUG: ANSIColors.cyan + '%(levelname)s:' + ANSIColors.white + '\t %(asctime)s %(module)s' + ANSIColors.cyan + ' %(message)s' + ANSIColors.reset, + logging.DEBUG: ANSIColors.cyan + '%(levelname)s:' + ANSIColors.white + '\t %(module)s' + ANSIColors.cyan + ' %(message)s' + ANSIColors.reset, + logging.INFO: ANSIColors.green + '%(levelname)s: \t %(message)s' + ANSIColors.reset, logging.WARNING: ANSIColors.yellow + '%(levelname)s:' + ANSIColors.reset + '\t %(message)s', logging.ERROR: ANSIColors.red + '%(levelname)s:' + ANSIColors.reset + '\t %(message)s', - logging.CRITICAL: ANSIColors.bold_red + '%(levelname)s:' + ANSIColors.reset + '\t %(message)s' + logging.CRITICAL: ANSIColors.bold_red + '%(levelname)s:' + ANSIColors.reset + ' %(message)s' } @@ -56,10 +57,12 @@ def logger_init(startup_logging_level: str = 'INFO') -> logging.Logger: logging.Logger: returns a Logger with custom formatter/handler and level set (see Args). """ - # access the 'global' logger - logger = logging.getLogger(__name__) + logger = logging.getLogger('api') - formatter = CustomFormatter(level_formats=level_formats) + # reduce datatimestamp {asctime} to HH:MM:SS without msec + date_format = '%H:%M:%S' + + formatter = CustomFormatter(level_formats=level_formats,datefmt=date_format) handler = logging.StreamHandler(sys.stdout) handler.setFormatter(formatter) diff --git a/api/tooling/set_log_level.py b/api/tooling/set_log_level.py index 7b2eee4..0f09236 100644 --- a/api/tooling/set_log_level.py +++ b/api/tooling/set_log_level.py @@ -1,4 +1,6 @@ import logging +import os +from time import sleep def set_log_level(level_str: str) -> None: """ @@ -10,7 +12,7 @@ def set_log_level(level_str: str) -> None: """ # access the 'global' logger - logger = logging.getLogger(__name__) + logger = logging.getLogger('api') level_str = level_str.upper() @@ -25,7 +27,10 @@ def set_log_level(level_str: str) -> None: logger.debug(f'level_str = {level_str}') if not level_str in log_levels: - level_str = 'DEBUG' + logger.critical('unrecognized LOG_LEVEL, FastAPI will probably throw up, exiting.') + logger.debug('sleeping for 5 seconds to reduce thrashing.') + sleep(5) + os.sys.exit() logger.setLevel(logging.getLevelName(level_str))