From f4ba64f1867759dbd35f11a9da66feada0381d82 Mon Sep 17 00:00:00 2001 From: "Maxime Alves LIRMM@home" Date: Thu, 12 Nov 2020 17:56:05 +0100 Subject: [PATCH] [debug] added /halfapi/log as a debug route that generates all type of logs --- halfapi/app.py | 9 ++++++++- halfapi/lib/routes.py | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/halfapi/app.py b/halfapi/app.py index 0088963..f12aa0e 100644 --- a/halfapi/app.py +++ b/halfapi/app.py @@ -9,6 +9,8 @@ It defines the following globals : - application (the asgi application itself - a starlette object) """ +import logging + # asgi framework from starlette.applications import Starlette from starlette.authentication import UnauthenticatedUser @@ -27,9 +29,10 @@ from halfapi.lib.jwt_middleware import JWTAuthenticationBackend from halfapi.lib.responses import (ORJSONResponse, UnauthorizedResponse, NotFoundResponse, InternalServerErrorResponse, NotImplementedResponse) -from halfapi.lib.routes import gen_starlette_routes, api_routes +from halfapi.lib.routes import gen_starlette_routes, api_routes, debug_routes from halfapi.lib.schemas import get_api_routes, schema_json, get_acls +logger = logging.getLogger('uvicorn.asgi') routes = [ Route('/', get_api_routes) ] @@ -43,6 +46,10 @@ routes += [ Route('/halfapi/acls', get_acls) ] +if not PRODUCTION: + for route in debug_routes(): + routes.append( route ) + for route in gen_starlette_routes(DOMAINSDICT()): routes.append(route) diff --git a/halfapi/lib/routes.py b/halfapi/lib/routes.py index 0f21f3d..cc81901 100644 --- a/halfapi/lib/routes.py +++ b/halfapi/lib/routes.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +from datetime import datetime from functools import wraps import logging from typing import Callable, List, Dict, Generator @@ -7,6 +8,7 @@ from types import ModuleType, FunctionType from starlette.exceptions import HTTPException from starlette.routing import Route from starlette.requests import Request +from starlette.responses import Response from halfapi.lib.domain import gen_domain_routes, VERBS @@ -140,3 +142,14 @@ def api_acls(request): res[domain][acl_name] = fct_result return res + + +def debug_routes(): + async def debug_log(request: Request, *args, **kwargs): + logger.debug('debuglog# %s', {datetime.now().isoformat()}) + logger.info('debuglog# %s', {datetime.now().isoformat()}) + logger.warning('debuglog# %s', {datetime.now().isoformat()}) + logger.error('debuglog# %s', {datetime.now().isoformat()}) + logger.critical('debuglog# %s', {datetime.now().isoformat()}) + return Response('') + yield Route('/halfapi/log', debug_log)