Skip to content

Commit

Permalink
logging refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ckuhtz committed Jul 16, 2024
1 parent 9dbf757 commit bf146af
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 34 deletions.
22 changes: 10 additions & 12 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fastapi import FastAPI
import uvicorn

from tooling import check_env_vars, set_log_level, logger_init
from tooling import check_env_vars, set_log_level, logger
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
Expand All @@ -25,15 +25,13 @@

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

logger = logger_init() # specify 'DEBUG' if early debugging (env) is needed

# check env and use defaults if not present

env = check_env_vars(logger)
env = check_env_vars()

# set logger level based on what we got back

set_log_level(logger, env['LOG_LEVEL'])
set_log_level(env['LOG_LEVEL'])

# dump environment we care about

Expand All @@ -42,13 +40,13 @@

# add REST routes

api.include_router(init_configuration_router(logger=logger), prefix='/config')
logger.debug('route added: /config')
api.include_router(init_database_router(logger=logger), prefix='/db')
logger.debug('route added: /db')
api.include_router(init_swissarmy_router(logger=logger), prefix='/internal', include_in_schema=False) # undocumented
logger.debug('route added: /internal (undocumented)')
# api.include_router(test_router(logger=logger), prefix='/test')
api.include_router(init_configuration_router, prefix='/config')
logger.debug('/config route added')
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
logger.debug('/internal route added (undocumented)')
# api.include_router(test_router, prefix='/test')
# logger.debug('/test route defined.')

# start API
Expand Down
3 changes: 1 addition & 2 deletions api/routers/configuration/delete.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
## Delete Configuration operation

import json
import logging
from ...tooling.logger_init import logger
from fastapi import Request, Response, status, Query
from ..database.redis import check_conf_server


async def delete_config(
logger: logging.Logger,
request: Request,
response: Response,
config_op: str,
Expand Down
3 changes: 1 addition & 2 deletions api/routers/configuration/get.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Get Configuration operation

import logging
from ...tooling.logger_init import logger
from fastapi import Request, Response, status, Query
from ..database.redis import check_conf_server

async def get_config(
logger: logging.Logger,
request: Request,
response: Response,
config_op: str,
Expand Down
23 changes: 16 additions & 7 deletions api/routers/configuration/operations.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
# Configuration operations

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

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

def init_router(logger: logging.Logger):
def init_router():

router = APIRouter()
base_path = '/config'

op = 'GET'
router.get("/", response_model=dict)(get_config)
logger.debug(f'added {op} {base_path}/ ')
router.get("/get", response_model=dict)(get_config)
logger.debug(f'added {op} {base_path}/get ')

router.get("/", response_model=dict)(get_config(logger=logger))
router.get("/get", response_model=dict)(get_config(logger=logger))
router.post("/", response_model=dict)(put_config(logger=logger))
router.post("/put", response_model=dict)(put_config(logger=logger))
router.post("/delete", response_model=dict)(delete_config(logger=logger))
op = 'POST'
router.post("/", response_model=dict)(put_config)
logger.debug(f'added {op} {base_path}/ ')
router.post("/put", response_model=dict)(put_config)
logger.debug(f'added {op} {base_path}/put ')
router.post("/delete", response_model=dict)(delete_config)
logger.debug(f'added {op} {base_path}/delete ')

return router
3 changes: 1 addition & 2 deletions api/routers/configuration/put.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Put Configuration operation

import json
import logging
from ...tooling.logger_init import logger
from fastapi import Request, Response, status, Query
from ..database.redis import check_conf_server

async def put_config(
logger: logging.Logger,
request: Request,
response: Response,
config_op: str,
Expand Down
1 change: 1 addition & 0 deletions api/routers/configuration/update.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Update Configuration operation

import json
from ...tooling.logger_init import logger
from fastapi import Request, Response, status, Query
from ..database.redis import check_conf_server

Expand Down
6 changes: 3 additions & 3 deletions api/routers/database/operations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Database operations

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

def init_router(logger: logging.Logger):
def init_router():

router = APIRouter()

return router
return router
6 changes: 3 additions & 3 deletions api/routers/internal/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# WARNING:
# unsupported for operations outside of API itself, do not call from outside of API codebase

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

def init_router(logger: logging.Logger):
def init_router():

router = APIRouter()

return router
return router
2 changes: 1 addition & 1 deletion api/tooling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

from .check_env_vars import check_env_vars
from .set_log_level import set_log_level
from .logger_init import logger_init
from .logger_init import logger
6 changes: 4 additions & 2 deletions api/tooling/logger_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ANSIColors:
logging.CRITICAL: ANSIColors.bold_red + '%(levelname)s:' + ANSIColors.reset + '\t %(message)s'
}

def logger_init(startup_logging_level: str = 'INFO') -> logging.Logger:
def define_logger(startup_logging_level: str = 'INFO') -> logging.Logger:
"""
Initialize logging.Logger with custom logging format based on logging severity level.
Expand All @@ -59,4 +59,6 @@ def logger_init(startup_logging_level: str = 'INFO') -> logging.Logger:

logger.setLevel(logging.getLevelName(startup_logging_level))

return logger
return logger

logger = define_logger() # specify 'DEBUG' if early debugging (env) is needed

0 comments on commit bf146af

Please sign in to comment.