Skip to content

Commit 331fa61

Browse files
fix(medcat-service) - Fix access log format not being set using uvicorn worker (#199)
1 parent cb3d7c5 commit 331fa61

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
log_config = {
2+
"version": 1,
3+
"disable_existing_loggers": False,
4+
"formatters": {
5+
"access": {
6+
"()": "uvicorn.logging.AccessFormatter",
7+
"fmt": '%(asctime)s [ACCESS] - %(client_addr)s - "%(request_line)s" %(status_code)s',
8+
"use_colors": True,
9+
},
10+
"default": {
11+
"()": "uvicorn.logging.DefaultFormatter",
12+
"fmt": "%(asctime)s [%(levelname)s] %(name)s: %(message)s",
13+
"use_colors": True,
14+
},
15+
},
16+
"handlers": {
17+
"access": {
18+
"class": "logging.StreamHandler",
19+
"formatter": "access",
20+
},
21+
"default": {
22+
"formatter": "default",
23+
"class": "logging.StreamHandler",
24+
},
25+
},
26+
"loggers": {
27+
# "root": {
28+
# "handlers": ["default"],
29+
# "level": settings.app_log_level,
30+
# },
31+
"uvicorn": {"handlers": ["default"], "level": "DEBUG", "propagate": True},
32+
"uvicorn.access": {"handlers": ["access"], "level": "INFO", "propagate": False},
33+
"uvicorn.error": {"level": "INFO", "propagate": False},
34+
},
35+
}

medcat-service/medcat_service/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
import logging
2+
import logging.config
3+
14
import gradio as gr
25
from fastapi import FastAPI, Request
36
from fastapi.responses import JSONResponse
47

58
from medcat_service.demo.gradio_demo import io
69
from medcat_service.dependencies import get_settings
10+
from medcat_service.log_config import log_config
711
from medcat_service.routers import admin, health, process
812
from medcat_service.types import HealthCheckFailedException
913

1014
settings = get_settings()
1115

16+
logging.config.dictConfig(log_config)
17+
1218
app = FastAPI(
1319
title="MedCAT Service",
1420
summary="MedCAT Service",
@@ -40,4 +46,5 @@ async def healthcheck_failed_exception_handler(request: Request, exc: HealthChec
4046
# Only run this when directly executing `python main.py` for local dev.
4147
import os
4248
import uvicorn
49+
4350
uvicorn.run("medcat_service.main:app", host="0.0.0.0", port=int(os.environ.get("SERVER_PORT", 8000)))

medcat-service/start_service_production.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ if [ -z ${SERVER_WORKER_TIMEOUT+x} ]; then
3333
echo "SERVER_WORKER_TIMEOUT is unset -- setting to default (sec): $SERVER_WORKER_TIMEOUT";
3434
fi
3535

36-
36+
# Note - SERVER_ACCESS_LOG_FORMAT is unused when worker-class is set to UvicornWorker
3737
SERVER_ACCESS_LOG_FORMAT="%(t)s [ACCESS] %(h)s \"%(r)s\" %(s)s \"%(f)s\" \"%(a)s\""
3838

3939
# start the server

0 commit comments

Comments
 (0)