Skip to content

Commit

Permalink
feat(logging): add logging file build_image
Browse files Browse the repository at this point in the history
  • Loading branch information
brucetony committed Mar 20, 2024
1 parent 51f28ef commit 1d77745
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
21 changes: 21 additions & 0 deletions gateway/server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
"""Methods for verifying auth."""
import json
import logging.config
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Annotated

import requests
Expand All @@ -17,6 +21,22 @@
from gateway.routers.metadata import metadata_router
from gateway.routers.results import results_router


@asynccontextmanager
async def lifespan(app: FastAPI):
"""Actions for lifespan of API."""
root_dir = Path.cwd().parent
root_dir.joinpath("logs").mkdir(parents=True, exist_ok=True)

log_config_path = root_dir.joinpath("logging.json")
with open(log_config_path, "r") as logf:
log_config = json.load(logf)

logging.config.dictConfig(log_config)

yield


# API metadata
tags_metadata = [
{"name": "Results", "description": "Endpoints for the Results service."},
Expand All @@ -34,6 +54,7 @@
# Auth fill client ID for the docs with the below value
"clientId": realm_idp_settings.client_id, # default client-id is Keycloak
},
lifespan=lifespan
)

app.add_middleware(
Expand Down
38 changes: 38 additions & 0 deletions logging.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"simple": {
"format": "%(levelname)s: %(message)s"
},
"detailed": {
"format": "%(levelname)s - %(module)s:L%(lineno)d - %(asctime)s - %(message)s",
"datefmt": "%Y-%m-%dT%H:%M:%S%z"
}
},
"handlers": {
"stderr": {
"class": "logging.StreamHandler",
"level": "WARNING",
"formatter": "simple",
"stream": "ext://sys.stderr"
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "detailed",
"filename": "../logs/node_hub_api_adapter.log",
"maxBytes": 1000000,
"backupCount": 5
}
},
"loggers": {
"root": {
"level": "DEBUG",
"handlers": [
"stderr",
"file"
]
}
}
}

0 comments on commit 1d77745

Please sign in to comment.