diff --git a/log_config.json b/log_config.json new file mode 100644 index 0000000..06591ed --- /dev/null +++ b/log_config.json @@ -0,0 +1,46 @@ +{ + "version": 1, + "disable_existing_loggers": false, + "formatters": { + "default": { + "()": "uvicorn.logging.DefaultFormatter", + "fmt": "%(asctime)s - %(levelprefix)s (%(funcName)s) %(message)s", + "use_colors": null + }, + "access": { + "()": "uvicorn.logging.AccessFormatter", + "fmt": "%(asctime)s - %(levelprefix)s %(client_addr)s - \"%(request_line)s\" %(status_code)s" + } + }, + "handlers": { + "default": { + "formatter": "default", + "class": "logging.StreamHandler", + "stream": "ext://sys.stderr" + }, + "access": { + "formatter": "access", + "class": "logging.StreamHandler", + "stream": "ext://sys.stdout" + } + }, + "loggers": { + "uvicorn": { + "handlers": [ + "default" + ], + "level": "INFO", + "propagate": false + }, + "uvicorn.error": { + "level": "DEBUG" + }, + "uvicorn.access": { + "handlers": [ + "access" + ], + "level": "INFO", + "propagate": false + } + } +} \ No newline at end of file diff --git a/main.py b/main.py index f7bd8d6..ef24d47 100644 --- a/main.py +++ b/main.py @@ -16,6 +16,13 @@ import uuid from typing import Dict, Any from pathlib import Path +import logging +from anyio import open_file + + +# Log to uvicorn general-purpose server logger +logger = logging.getLogger('uvicorn.error') + app = FastAPI( title="NetworkX Graph API", @@ -283,6 +290,7 @@ async def get_node_type_counts(graph_id: str): Returns: A JSON object containing the count of nodes for each type. """ + logger.debug("Calling.") # Check if graph ID exists in registry if graph_id not in graph_registry: raise HTTPException(status_code=404, detail="Graph ID not found") @@ -290,9 +298,12 @@ async def get_node_type_counts(graph_id: str): try: # Load the graph from file file_path = graph_registry[graph_id] - with open(file_path, 'r') as f: - data = json.load(f) + async with await open_file(file_path, 'r') as f: + logger.debug("Opening file.") + content = await f.read() + logger.debug("Closing file.") + data = json.loads(content) # Load as NetworkX graph graph = nx.node_link_graph(data) @@ -328,6 +339,7 @@ async def get_edge_type_counts(graph_id: str): Returns: A JSON object containing the count of edges for each type. """ + logger.debug("Calling.") # Check if graph ID exists in registry if graph_id not in graph_registry: raise HTTPException(status_code=404, detail="Graph ID not found") @@ -335,9 +347,12 @@ async def get_edge_type_counts(graph_id: str): try: # Load the graph from file file_path = graph_registry[graph_id] - with open(file_path, 'r') as f: - data = json.load(f) + async with await open_file(file_path, 'r') as f: + logger.debug("Opening file.") + content = await f.read() + logger.debug("Closing file.") + data = json.loads(content) # Load as NetworkX graph graph = nx.node_link_graph(data)