Skip to content

Commit a8a5681

Browse files
committed
working & improved logging
1 parent 33e24d2 commit a8a5681

7 files changed

Lines changed: 587 additions & 92 deletions

File tree

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.10-slim as base
1+
FROM python:3.10-slim AS base
22

33
ARG api_port
44
ENV UVICORN_PORT ${api_port}
@@ -23,9 +23,9 @@ RUN pip install --no-cache-dir -r requirements.txt
2323
COPY ./src /project/src
2424

2525
# production image
26-
FROM base as production
26+
FROM base AS production
2727
# Creates a non-root user with an explicit UID and adds permission to access the /project folder
2828
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /project
2929
USER appuser
3030

31-
CMD ["uvicorn", "src.core.server:app", "--proxy-headers", "--host", "0.0.0.0"]
31+
CMD ["uvicorn", "src.core.server:app", "--proxy-headers", "--host", "0.0.0.0", "--log-level", "warning"]

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ services:
3232
args:
3333
root_path: /
3434
api_port: 5000
35-
command: uvicorn src.core.server:app --host 0.0.0.0 --reload --reload-include src/*
35+
command: uvicorn src.core.server:app --host 0.0.0.0 --reload --reload-include src/* --log-level warning
3636
container_name: bd-dev-api
3737
environment:
3838
- sql_uri=mysql+asyncmy://root:root_bot_buster@mysql/playerdata

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ description = "Add your description here"
55
readme = "README.md"
66
requires-python = ">=3.10"
77
dependencies = [
8-
"aiohttp==3.9.5",
8+
"aiohttp>=3.9.5",
99
"asyncmy==0.2.8",
10-
"fastapi==0.100.1",
10+
"fastapi[standard]>=0.115.5",
1111
"pandas>=2.0.3",
12+
"prometheus-client>=0.21.0",
1213
"python-dotenv==1.0.0",
1314
"sqlalchemy==2.0.19",
14-
"starlette-prometheus==0.9.0",
15-
"uvicorn==0.23.1",
15+
"starlette-prometheus>=0.9.0",
1616
]
1717

1818
[dependency-groups]

requirements.txt

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,60 @@ aiosignal==1.3.1
77
annotated-types==0.7.0
88
# via pydantic
99
anyio==4.6.2.post1
10-
# via starlette
10+
# via
11+
# httpx
12+
# starlette
13+
# watchfiles
1114
asyncmy==0.2.8
1215
# via core-api (pyproject.toml)
1316
attrs==24.2.0
1417
# via aiohttp
18+
certifi==2024.8.30
19+
# via
20+
# httpcore
21+
# httpx
1522
click==8.1.7
16-
# via uvicorn
17-
fastapi==0.100.1
23+
# via
24+
# typer
25+
# uvicorn
26+
dnspython==2.7.0
27+
# via email-validator
28+
email-validator==2.2.0
29+
# via fastapi
30+
fastapi==0.115.5
1831
# via core-api (pyproject.toml)
32+
fastapi-cli==0.0.5
33+
# via fastapi
1934
frozenlist==1.5.0
2035
# via
2136
# aiohttp
2237
# aiosignal
2338
greenlet==3.1.1
2439
# via sqlalchemy
2540
h11==0.14.0
41+
# via
42+
# httpcore
43+
# uvicorn
44+
httpcore==1.0.7
45+
# via httpx
46+
httptools==0.6.4
2647
# via uvicorn
48+
httpx==0.28.0
49+
# via fastapi
2750
idna==3.10
2851
# via
2952
# anyio
53+
# email-validator
54+
# httpx
3055
# yarl
56+
jinja2==3.1.4
57+
# via fastapi
58+
markdown-it-py==3.0.0
59+
# via rich
60+
markupsafe==3.0.2
61+
# via jinja2
62+
mdurl==0.1.2
63+
# via markdown-it-py
3164
multidict==6.1.0
3265
# via
3366
# aiohttp
@@ -36,41 +69,66 @@ numpy==2.1.3
3669
# via pandas
3770
pandas==2.2.3
3871
# via core-api (pyproject.toml)
39-
prometheus-client==0.12.0
40-
# via starlette-prometheus
72+
prometheus-client==0.21.0
73+
# via
74+
# core-api (pyproject.toml)
75+
# starlette-prometheus
4176
propcache==0.2.0
4277
# via yarl
4378
pydantic==2.10.2
4479
# via fastapi
4580
pydantic-core==2.27.1
4681
# via pydantic
82+
pygments==2.18.0
83+
# via rich
4784
python-dateutil==2.9.0.post0
4885
# via pandas
4986
python-dotenv==1.0.0
50-
# via core-api (pyproject.toml)
87+
# via
88+
# core-api (pyproject.toml)
89+
# uvicorn
90+
python-multipart==0.0.18
91+
# via fastapi
5192
pytz==2024.2
5293
# via pandas
94+
pyyaml==6.0.2
95+
# via uvicorn
96+
rich==13.9.4
97+
# via typer
98+
shellingham==1.5.4
99+
# via typer
53100
six==1.16.0
54101
# via python-dateutil
55102
sniffio==1.3.1
56103
# via anyio
57104
sqlalchemy==2.0.19
58105
# via core-api (pyproject.toml)
59-
starlette==0.27.0
106+
starlette==0.41.3
60107
# via
61108
# fastapi
62109
# starlette-prometheus
63-
starlette-prometheus==0.9.0
110+
starlette-prometheus==0.10.0
64111
# via core-api (pyproject.toml)
112+
typer==0.14.0
113+
# via fastapi-cli
65114
typing-extensions==4.12.2
66115
# via
67116
# fastapi
68117
# pydantic
69118
# pydantic-core
70119
# sqlalchemy
120+
# typer
71121
tzdata==2024.2
72122
# via pandas
73123
uvicorn==0.23.1
74-
# via core-api (pyproject.toml)
124+
# via
125+
# fastapi
126+
# fastapi-cli
127+
uvloop==0.21.0
128+
# via uvicorn
129+
watchfiles==1.0.0
130+
# via uvicorn
131+
websockets==14.1
132+
# via uvicorn
75133
yarl==1.18.0
76134
# via aiohttp

src/core/logging.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def filter(self, record):
3535
logging.basicConfig(level=logging.INFO, handlers=[handler])
3636

3737
# set imported loggers to warning
38-
logging.getLogger("requests").setLevel(logging.DEBUG)
39-
logging.getLogger("urllib3").setLevel(logging.DEBUG)
40-
logging.getLogger("uvicorn").setLevel(logging.DEBUG)
41-
logging.getLogger("apscheduler").setLevel(logging.WARNING)
42-
logging.getLogger("aiomysql").setLevel(logging.ERROR)
43-
logging.getLogger("asyncmy").setLevel(logging.ERROR)
44-
logging.getLogger("aiokafka").setLevel(logging.WARNING)
38+
# logging.getLogger("requests").setLevel(logging.DEBUG)
39+
# logging.getLogger("urllib3").setLevel(logging.DEBUG)
40+
# logging.getLogger("uvicorn").setLevel(logging.DEBUG)
41+
# logging.getLogger("apscheduler").setLevel(logging.WARNING)
42+
# logging.getLogger("aiomysql").setLevel(logging.ERROR)
43+
# logging.getLogger("asyncmy").setLevel(logging.ERROR)
44+
# logging.getLogger("aiokafka").setLevel(logging.WARNING)

src/core/server.py

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from src import api
1212
from starlette_prometheus import metrics, PrometheusMiddleware
13-
13+
from src.core.fastapi.middleware import LoggingMiddleware
1414
logger = logging.getLogger(__name__)
1515

1616

@@ -32,6 +32,7 @@ def make_middleware() -> list[Middleware]:
3232
allow_methods=["*"],
3333
allow_headers=["*"],
3434
),
35+
Middleware(LoggingMiddleware)
3536
]
3637
return middleware
3738

@@ -60,29 +61,6 @@ async def root():
6061
async def favicon():
6162
return {}
6263

63-
64-
@app.middleware("http")
65-
async def add_process_time_header(request: Request, call_next):
66-
start_time = time.time()
67-
response = await call_next(request)
68-
process_time = time.time() - start_time
69-
70-
query_params_list = [
71-
(key, value if key != "token" else "***")
72-
for key, value in request.query_params.items()
73-
]
74-
75-
url_path = request.url.path
76-
logger.debug(
77-
{
78-
"url": url_path,
79-
"params": query_params_list,
80-
"process_time": f"{process_time:.4f}",
81-
}
82-
)
83-
return response
84-
85-
8664
@app.exception_handler(RequestValidationError)
8765
async def validation_exception_handler(request: Request, exc: RequestValidationError):
8866
error = json.loads(exc.json())
@@ -95,12 +73,4 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
9573
"error": error,
9674
}
9775
)
98-
return JSONResponse(content={"detail": error}, status_code=422)
99-
100-
101-
# @app.on_event("startup")
102-
# async def startup_event():
103-
# if config.env != "DEV":
104-
# logger.info("startup initiated")
105-
# highscore_processor = HighscoreProcessor(batch_size=100)
106-
# asyncio.ensure_future(highscore_processor.start())
76+
return JSONResponse(content={"detail": error}, status_code=422)

0 commit comments

Comments
 (0)