Skip to content

Commit 0c2b8a9

Browse files
committed
API: Added access logs to gunicorn deployment.
Any future API logs will be also functional, both in uvicorn only and with gunicorn.
1 parent c43cd53 commit 0c2b8a9

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

dp3/api/internal/dp_logger.py

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def setup_logger(self, name: str, log_file: str):
5656
logger = logging.getLogger(name)
5757
logger.addHandler(log_handler)
5858
logger.setLevel(logging.INFO)
59+
logger.propagate = False
5960

6061
return logger
6162

dp3/api/main.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23

34
from fastapi import FastAPI
45
from fastapi.exception_handlers import request_validation_exception_handler
@@ -8,6 +9,16 @@
89
from dp3.api.internal.config import CONFIG, DATAPOINTS_INGESTION_URL_PATH, DP_LOGGER, ROOT_PATH
910
from dp3.api.routers import control, entity, root
1011

12+
uvicorn_logger = logging.getLogger("uvicorn")
13+
uvicorn_access_logger = logging.getLogger("uvicorn.access")
14+
gunicorn_error_logger = logging.getLogger("gunicorn.error")
15+
16+
if len(gunicorn_error_logger.handlers) > 0: # We are running in gunicorn
17+
logging.root.handlers = gunicorn_error_logger.handlers
18+
uvicorn_access_logger.handlers = gunicorn_error_logger.handlers
19+
elif len(uvicorn_logger.handlers) > 0: # We are running only in uvicorn
20+
logging.root.handlers = uvicorn_logger.handlers
21+
1122
# Create new FastAPI app
1223
app = FastAPI(root_path=ROOT_PATH)
1324

0 commit comments

Comments
 (0)