Skip to content

Commit

Permalink
refactor logging again, and a few other things
Browse files Browse the repository at this point in the history
  • Loading branch information
ckuhtz committed Jul 16, 2024
1 parent a973136 commit cd92296
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 46 deletions.
22 changes: 9 additions & 13 deletions api/api.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
# implement REST API using FastAPI

import sys
import logging
from fastapi import FastAPI
import uvicorn

from tooling.logger_init import logger
import tooling

from routers.configuration.operations import init_router as init_configuration_router
from routers.database.operations import init_router as init_database_router
from routers.internal.operations import init_router as init_swissarmy_router
# from routers.test.operations import router as test_router



# Define API app as 'api'

api = FastAPI()



if __name__ == '__main__':

# stubs follow, this should be read from redis kvs for instance, section 'hamframe'

# see https://fastapi.tiangolo.com/deployment/docker/#replication-number-of-processes for comment on worker counts

logger = tooling.logger_init()

# check env and use defaults if not present

env = check_env_vars()
env = tooling.check_env_vars()

# set logger level based on what we got back

set_log_level(env['LOG_LEVEL'])
tooling.set_log_level(env['LOG_LEVEL'])

# dump environment we care about

Expand All @@ -41,11 +38,11 @@

# add REST routes

api.include_router(init_configuration_router, prefix='/config')
api.include_router(init_configuration_router(), prefix='/config')
logger.debug('/config route added')
api.include_router(init_database_router, prefix='/db')
api.include_router(init_database_router(), prefix='/db')
logger.debug('/db route ad')
api.include_router(init_swissarmy_router, prefix='/internal', include_in_schema=False) # undocumented
api.include_router(init_swissarmy_router(), prefix='/internal', include_in_schema=False) # undocumented
logger.debug('/internal route added (undocumented)')
# api.include_router(test_router, prefix='/test')
# logger.debug('/test route defined.')
Expand All @@ -57,7 +54,6 @@
host=str(env['LISTENER_HOST']),
port=int(env['LISTENER_PORT']),
workers=int(env['LISTENER_WORKERS']),
log_level=str(env['LOG_LEVEL']),
logger=logger
log_level=str(env['LOG_LEVEL'])
)

2 changes: 1 addition & 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
# from ...tooling.logger import logger
from fastapi import Request, Response, status, Query
from ..database.redis import check_conf_server

Expand Down
2 changes: 1 addition & 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.logger_init import logger
# from ...tooling import logger
from fastapi import Request, Response, status, Query
from ..database.redis import check_conf_server

Expand Down
7 changes: 5 additions & 2 deletions api/routers/configuration/operations.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Configuration operations

from api.tooling.logger_init import logger
from fastapi import APIRouter
import logging

from .get import get_config
from .put import put_config
from .delete import delete_config

def init_router():
def init_router() -> APIRouter:

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

router = APIRouter()
base_path = '/config'
Expand Down
2 changes: 1 addition & 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
# from ...tooling.logger_init import logger
from fastapi import Request, Response, status, Query
from ..database.redis import check_conf_server

Expand Down
2 changes: 1 addition & 1 deletion api/routers/database/couchbase.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster, ClusterOptions
from couchbase.exceptions import CouchbaseException, DocumentNotFoundException, TimeoutException
from api.tooling.logger import logger
# from api.tooling.logger import logger

def check_couchbase(couchbase_param):
"""
Expand Down
4 changes: 2 additions & 2 deletions api/routers/database/operations.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Database operations

from ...tooling.logger import logger
# from ...tooling.logger import logger
from fastapi import APIRouter

def init_router():
def init_router() -> APIRouter:

router = APIRouter()

Expand Down
2 changes: 1 addition & 1 deletion api/routers/database/redis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import redis
import urllib.parse
from ...tooling.logger import logger
# from ...tooling.logger import logger

def check_conf_server(host_param):
"""
Expand Down
4 changes: 2 additions & 2 deletions api/routers/internal/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# WARNING:
# unsupported for operations outside of API itself, do not call from outside of API codebase

from api.tooling.logger_init import logger
# from api.tooling.logger_init import logger
from fastapi import APIRouter

def init_router():
def init_router() -> APIRouter:

router = APIRouter()

Expand Down
4 changes: 4 additions & 0 deletions api/tooling/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# __init__.py

from .logger_init import logger_init
from .set_log_level import set_log_level
from .check_env_vars import check_env_vars
19 changes: 9 additions & 10 deletions api/tooling/check_env_vars.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ast import Dict
import os
from .logger import logger
import logging

def check_env_vars() -> Dict:
"""
Expand All @@ -12,6 +12,9 @@ def check_env_vars() -> Dict:
Dict: returns a dictionary of environment variables and their assigned values. NOT typesafe. Consumer is resonsible for typecasting as needed.
"""

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

vars = [
'REDIS_HOST',
'REDIS_PORT',
Expand All @@ -34,21 +37,17 @@ def check_env_vars() -> Dict:

env = {}

if logger:
logger.debug(f'all_env_vars: {all_env_vars}')
logger.debug(f'env: {env}')
logger.debug(f'all_env_vars: {all_env_vars}')
logger.debug(f'env: {env}')

for var in vars:
if var in all_env_vars:
env[var] = all_env_vars[var]
if logger:
logger.debug(f'env: {var}={env[var]}')
logger.debug(f'env: {var}={env[var]}')
else:
env[var] = var_defaults[var]
if logger:
logger.debug(f'env: {var}={env[var]} (default)')
logger.debug(f'env: {var}={env[var]} (default)')

if logger:
logger.debug(f'env: {env}')
logger.debug(f'env: {env}')

return env
12 changes: 5 additions & 7 deletions api/tooling/logger_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,17 @@ 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__)

formatter = CustomFormatter(level_formats=level_formats)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)

logger = logging.getLogger(__name__)

logger.addHandler(handler)

# set_log_level(logger, 'debug') # normally not needed, enable for debugging of env variables only

logger.setLevel(logging.getLevelName(startup_logging_level))

return logger



logger = logger_init() # specify 'DEBUG' if early debugging (env) is needed
return logger
12 changes: 7 additions & 5 deletions api/tooling/set_log_level.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from logging import getLevelName
from .logger import logger

import logging

def set_log_level(level_str: str) -> None:
"""
Expand All @@ -10,6 +8,10 @@ def set_log_level(level_str: str) -> None:
logger (Logger): reference to an existing Logger
level_str (str): case-insensitive text string for logging level, e.g. 'debug' for LOG_DEBUG.
"""

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

level_str = level_str.upper()

log_levels = [
Expand All @@ -25,6 +27,6 @@ def set_log_level(level_str: str) -> None:
if not level_str in log_levels:
level_str = 'DEBUG'

logger.setLevel(getLevelName(level_str))
logger.setLevel(logging.getLevelName(level_str))

logger.info(f'logging level: {level_str} ({getLevelName(level_str)})')
logger.info(f'logging level: {level_str} ({logging.getLevelName(level_str)})')

0 comments on commit cd92296

Please sign in to comment.