Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ckuhtz committed Jul 16, 2024
1 parent cd92296 commit 0689142
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 19 deletions.
3 changes: 2 additions & 1 deletion api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@

# 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

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
Expand Down
4 changes: 3 additions & 1 deletion api/routers/configuration/delete.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion api/routers/configuration/get.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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}')
Expand Down
4 changes: 3 additions & 1 deletion api/routers/configuration/put.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -14,6 +14,8 @@ async def put_config(
config_section: str = Query(None, alias='section')
):

logger = logging.getLogger('api')

route_path = request.url.path


Expand Down
2 changes: 1 addition & 1 deletion api/routers/configuration/update.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 6 additions & 1 deletion api/routers/database/couchbase.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion api/routers/database/operations.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion api/routers/database/redis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import redis
import urllib.parse
# from ...tooling.logger import logger
import logging



def check_conf_server(host_param):
"""
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions api/routers/internal/get_config_section.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import requests
import logging

def get_config_section(api_url, redis_url, instance, section):

logger = logging.getLogger('api')

params = {

}
Expand Down
6 changes: 5 additions & 1 deletion api/routers/internal/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "()")

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion api/tooling/check_env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def check_env_vars() -> Dict:
"""

# access the 'global' logger
logger = logging.getLogger(__name__)
logger = logging.getLogger('api')

vars = [
'REDIS_HOST',
Expand Down
17 changes: 10 additions & 7 deletions api/tooling/logger_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}


Expand All @@ -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)

Expand Down
9 changes: 7 additions & 2 deletions api/tooling/set_log_level.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import os
from time import sleep

def set_log_level(level_str: str) -> None:
"""
Expand All @@ -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()

Expand All @@ -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))

Expand Down

0 comments on commit 0689142

Please sign in to comment.