make log output configurable

This commit is contained in:
Sarah Hoffmann
2025-09-10 20:11:46 +02:00
parent 177b16b89b
commit 3d0867ff16
6 changed files with 49 additions and 16 deletions

View File

@@ -122,7 +122,8 @@ class FileLoggingMiddleware:
""" Middleware to log selected requests into a file.
"""
def __init__(self, file_name: str):
def __init__(self, file_name: str, logstr: str):
self.logstr = logstr + '\n'
self.fd = open(file_name, 'a', buffering=1, encoding='utf8')
async def process_request(self, req: Request, _: Response) -> None:
@@ -151,8 +152,7 @@ class FileLoggingMiddleware:
qs[param] = qs[param].replace(tzinfo=None)\
.isoformat(sep=' ', timespec='milliseconds')
self.fd.write(("[{start}] {total_time:.4f} {results_total} "
'{endpoint} "{query_string}"\n').format_map(qs))
self.fd.write(self.logstr.format_map(qs))
class APIMiddleware:
@@ -201,7 +201,7 @@ def get_application(project_dir: Path,
middleware: List[Any] = [apimw]
log_file = apimw.config.LOG_FILE
if log_file:
middleware.append(FileLoggingMiddleware(log_file))
middleware.append(FileLoggingMiddleware(log_file, apimw.config.LOG_FORMAT))
app = App(cors_enable=apimw.config.get_bool('CORS_NOACCESSCONTROL'),
middleware=middleware)

View File

@@ -87,9 +87,10 @@ class FileLoggingMiddleware(BaseHTTPMiddleware):
""" Middleware to log selected requests into a file.
"""
def __init__(self, app: Starlette, file_name: str = ''):
def __init__(self, app: Starlette, file_name: str = '', logstr: str = ''):
super().__init__(app)
self.fd = open(file_name, 'a', buffering=1, encoding='utf8')
self.logstr = logstr + '\n'
async def dispatch(self, request: Request,
call_next: RequestResponseEndpoint) -> Response:
@@ -114,8 +115,7 @@ class FileLoggingMiddleware(BaseHTTPMiddleware):
qs[param] = qs[param].replace(tzinfo=None)\
.isoformat(sep=' ', timespec='milliseconds')
self.fd.write(("[{start}] {total_time:.4f} {results_total} "
'{endpoint} "{query_string}"\n').format_map(qs))
self.fd.write(self.logstr.format_map(qs))
return response
@@ -149,7 +149,8 @@ def get_application(project_dir: Path,
log_file = config.LOG_FILE
if log_file:
middleware.append(Middleware(FileLoggingMiddleware, file_name=log_file)) # type: ignore
middleware.append(Middleware(FileLoggingMiddleware, file_name=log_file, # type: ignore
logstr=config.LOG_FORMAT))
exceptions: Dict[Any, Callable[[Request, Exception], Awaitable[Response]]] = {
TimeoutError: timeout_error,