Skip to content
This repository was archived by the owner on May 7, 2026. It is now read-only.

Commit 1147cad

Browse files
authored
implemented simple logging (#14)
* implemented simple logging * added helm values for logging information, which get set as environment variables in the created container. These then get picked up by pydantic BaseSettings and used to set logging config, with a try/except block to catch invalid settings and set a default logger. * fixed typo logging-> logger inside middleware
1 parent 04ac23d commit 1147cad

4 files changed

Lines changed: 46 additions & 17 deletions

File tree

helm/diffcalc-api/templates/deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ spec:
3636
env:
3737
- name: mongo_url
3838
value: {{ include "diffcalc-api.mongoUrl" . }}
39+
- name: logging_level
40+
value: {{ .Values.logging.level }}
41+
- name: logging_format
42+
value: {{ .Values.logging.format }}
3943
ports:
4044
- name: http
4145
containerPort: 8000

helm/diffcalc-api/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,7 @@ mongodb:
9494
cpu: 2
9595
requests:
9696
cpu: 1
97+
98+
logging:
99+
level: "WARN"
100+
format: "[%(asctime)s] %(levelname)s:%(message)s"

src/diffcalc_API/config.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
1+
import logging
2+
13
from pydantic import BaseSettings
24

35
from ._version_git import __version__
46

7+
release = __version__
8+
9+
# The short X.Y version.
10+
if "+" in release:
11+
# Not on a tag
12+
version = "master"
13+
else:
14+
version = release
15+
16+
17+
class Settings(BaseSettings):
18+
mongo_url: str = "localhost:27017"
19+
api_version = version
20+
logging_level: str = "WARN"
21+
logging_format: str = "[%(asctime)s] %(levelname)s:%(message)s"
22+
23+
24+
settings = Settings()
25+
try:
26+
logging.basicConfig(level=settings.logging_level, format=settings.logging_format)
27+
except ValueError:
28+
logging.basicConfig(level="WARN")
29+
logging.warn(
30+
f"{settings.logging_level} is not a valid logging level "
31+
+ "(See logging python library for details). Initializing basic logging."
32+
)
33+
534
SAVE_PICKLES_FOLDER = "/"
635
VECTOR_PROPERTIES = ["n_hkl", "n_phi", "surf_nhkl", "surf_nphi"]
736
CONSTRAINTS_WITH_NO_VALUE = {"a_eq_b", "bin_eq_bout", "mu_is_gam", "bisect"}
@@ -26,17 +55,3 @@
2655
"bisect",
2756
"omega",
2857
}
29-
30-
release = __version__
31-
32-
# The short X.Y version.
33-
if "+" in release:
34-
# Not on a tag
35-
version = "master"
36-
else:
37-
version = release
38-
39-
40-
class Settings(BaseSettings):
41-
mongo_url: str = "localhost:27017"
42-
api_version = version

src/diffcalc_API/server.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import traceback
23
from typing import Optional
34

@@ -12,8 +13,8 @@
1213
from diffcalc_API.errors.ub import responses as ub_responses
1314
from diffcalc_API.stores.protocol import get_store, setup_store
1415

16+
logger = logging.getLogger(__name__)
1517
config = Settings()
16-
1718
setup_store("diffcalc_API.stores.mongo.MongoHklCalcStore")
1819

1920
app = FastAPI(
@@ -31,6 +32,9 @@
3132

3233
@app.exception_handler(DiffcalcException)
3334
async def diffcalc_exception_handler(request: Request, exc: DiffcalcException):
35+
tb = traceback.format_exc()
36+
logger.warn(f"Diffcalc Exception caught by middleware: {tb}")
37+
3438
return responses.JSONResponse(
3539
status_code=400,
3640
content={"message": str(exc), "type": str(type(exc))},
@@ -39,6 +43,9 @@ async def diffcalc_exception_handler(request: Request, exc: DiffcalcException):
3943

4044
@app.exception_handler(DiffcalcAPIException)
4145
async def http_exception_handler(request: Request, exc: DiffcalcAPIException):
46+
tb = traceback.format_exc()
47+
logger.error(f"Diffcalc API Exception caught by middleware: {tb}")
48+
4249
return responses.JSONResponse(
4350
status_code=exc.status_code,
4451
content={"message": exc.detail, "type": str(type(exc))},
@@ -50,9 +57,8 @@ async def server_exceptions_middleware(request: Request, call_next):
5057
try:
5158
return await call_next(request)
5259
except Exception as e:
53-
# TODO: implement proper logging
5460
tb = traceback.format_exc()
55-
print(tb)
61+
logger.error(f"General Exception caught by middleware: {tb}")
5662

5763
return responses.JSONResponse(
5864
status_code=500,

0 commit comments

Comments
 (0)