Skip to content

Commit

Permalink
fix log imports
Browse files Browse the repository at this point in the history
  • Loading branch information
xgui3783 committed Jan 15, 2024
1 parent 782e7e4 commit 11ccb46
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion api/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .decorators import data_decorator, router_decorator, async_router_decorator, name_to_fns_map
from .siibra_api_typing import ROLE_TYPE
from .logger import logger, access_logger
from .logger import logger as general_logger, access_logger
from .exceptions import *
from .storage import get_filename
10 changes: 5 additions & 5 deletions api/common/data_handlers/features/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from api.common import data_decorator, InsufficientParameters, NotFound, AmbiguousParameters, logger
from api.common import data_decorator, InsufficientParameters, NotFound, AmbiguousParameters, general_logger
from api.siibra_api_config import ROLE, SIIBRA_API_SHARED_DIR
from typing import List, Type, Any, Dict
from hashlib import md5
Expand Down Expand Up @@ -108,13 +108,13 @@ def get_single_feature_download_zip_path(feature_id: str, **kwargs):
try:
feat = siibra.features.Feature._get_instance_by_id(feature_id)
except Exception as e:
logger.error(f"Error finding single feature {feature_id=}, {str(e)}")
general_logger.error(f"Error finding single feature {feature_id=}, {str(e)}")
raise NotFound from e
try:
feat.export(str(full_filename))
return str(full_filename)
except Exception as e:
logger.error(f"Error export single feature {feature_id=}, {str(e)}")
general_logger.error(f"Error export single feature {feature_id=}, {str(e)}")
error_filename = full_filename.with_suffix(".error.zip")
with ZipFile(error_filename, "w") as zf:
zf.writestr("error.txt", f"Error exporting file for feature_id: {feature_id}: {str(e)}")
Expand Down Expand Up @@ -153,7 +153,7 @@ def get_all_all_features(*, space_id: str=None, parcellation_id: str=None, regio
)
except Exception as e:
err_str = str(e).replace('\n', ' ')
logger.warning(f"feature failed to be serialized. Params: space_id={space_id}, parcellation_id={parcellation_id}, region_id={region_id}. feature id: {f.id}. error: {err_str}")
general_logger.warning(f"feature failed to be serialized. Params: space_id={space_id}, parcellation_id={parcellation_id}, region_id={region_id}. feature id: {f.id}. error: {err_str}")
return re_features


Expand Down Expand Up @@ -184,7 +184,7 @@ def all_features(*, space_id: str, parcellation_id: str, region_id: str, type: s
)
except Exception as e:
err_str = str(e).replace('\n', ' ')
logger.warning(f"feature failed to be serialized. Params: space_id={space_id}, parcellation_id={parcellation_id}, region_id={region_id}. feature id: {f.id}, error: {err_str}")
general_logger.warning(f"feature failed to be serialized. Params: space_id={space_id}, parcellation_id={parcellation_id}, region_id={region_id}. feature id: {f.id}, error: {err_str}")
return re_features


Expand Down
13 changes: 7 additions & 6 deletions api/common/decorators.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from .siibra_api_typing import ROLE_TYPE
from api.common import logger
from functools import wraps, partial
import inspect
import asyncio
import time
from api.common.exceptions import (
from typing import Dict, Callable, Tuple

from .siibra_api_typing import ROLE_TYPE
from .logger import logger as general_logger
from .exceptions import (
FaultyRoleException,
)
from typing import Dict, Callable, Tuple

name_to_fns_map: Dict[str, Tuple[Callable, Callable]] = {}

Expand All @@ -33,15 +34,15 @@ def outer_wrapper(fn):

def celery_task_wrapper(self, *args, **kwargs):
if role == "worker":
logger.info(f"Task Received: {fn.__name__=}, {args=}, {kwargs=}")
general_logger.info(f"Task Received: {fn.__name__=}, {args=}, {kwargs=}")
return fn(*args, **kwargs)

return app.task(bind=True)(
wraps(fn)(celery_task_wrapper)
)
except ImportError as e:
errmsg = f"For worker role, celery must be installed as a dep"
logger.critical(errmsg)
general_logger.critical(errmsg)
raise ImportError(errmsg) from e
raise FaultyRoleException(f"role must be 'all', 'server' or 'worker', but it is {role}")
return outer_wrapper
Expand Down
4 changes: 2 additions & 2 deletions api/serialization/core/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
instance_to_model
)
from api.serialization.util.siibra import Region, Space, parcellations, RegionRelationAssessments
from api.common.logger import logger
from api.common import general_logger



Expand Down Expand Up @@ -149,7 +149,7 @@ def region_to_model(region: Region, *, min_flag: bool=False, detail: bool=False,
if centroids is not None and len(centroids) > 0:
centroid = centroids[0]
if len(centroids) > 1:
logger.warn(f"region {region.name!r} returned multiple centroids in space {space.name!r}. Returning the first one.")
general_logger.warn(f"region {region.name!r} returned multiple centroids in space {space.name!r}. Returning the first one.")

pev.has_annotation = HasAnnotation(
best_view_point=BestViewPoint(
Expand Down
10 changes: 5 additions & 5 deletions api/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .metrics import prom_metrics_resp, on_startup as metrics_on_startup, on_terminate as metrics_on_terminate
from .code_snippet import get_sourcecode

from ..common import logger, access_logger, NotFound, SapiBaseException, name_to_fns_map
from ..common import general_logger, access_logger, NotFound, SapiBaseException, name_to_fns_map
from ..siibra_api_config import GIT_HASH

siibra_version_header = "x-siibra-api-version"
Expand Down Expand Up @@ -295,15 +295,15 @@ async def middleware_access_log(request: Request, call_next):
"hit_cache": "cache_miss"
})
except Exception as e:
logger.critical(e)
general_logger.critical(e)

@siibra_api.exception_handler(RuntimeError)
async def exception_runtime(request: Request, exc: RuntimeError) -> JSONResponse:
"""Handling RuntimeErrors.
Most of the RuntimeErrors are thrown by the siibra-python library when other Services are not responding.
To be more resilient and not throw a simple and unplanned HTTP 500 response, this handler will return an HTTP 503
status."""
logger.warning(f"Error handler: exception_runtime: {str(exc)}")
general_logger.warning(f"Error handler: exception_runtime: {str(exc)}")
return JSONResponse(
status_code=503,
content={
Expand All @@ -315,13 +315,13 @@ async def exception_runtime(request: Request, exc: RuntimeError) -> JSONResponse
@siibra_api.exception_handler(SapiBaseException)
def exception_sapi(request: Request, exc: SapiBaseException):
"""Handle sapi errors"""
logger.warning(f"Error handler: exception_sapi: {str(exc)}")
general_logger.warning(f"Error handler: exception_sapi: {str(exc)}")
raise HTTPException(400, str(exc))

@siibra_api.exception_handler(Exception)
async def exception_other(request: Request, exc: Exception):
"""Catch all exception handler"""
logger.warning(f"Error handler: exception_other: {str(exc)}")
general_logger.warning(f"Error handler: exception_other: {str(exc)}")
return JSONResponse(
status_code=500,
content={
Expand Down
6 changes: 3 additions & 3 deletions api/server/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path
from api.siibra_api_config import ROLE, CELERY_CONFIG, NAME_SPACE, MONITOR_FIRSTLVL_DIR
from api.common.timer import RepeatTimer
from api.common import logger
from api.common import general_logger

class Singleton:
"""Timer singleton"""
Expand All @@ -27,7 +27,7 @@ def timed_du():
try:
dirs = os.listdir(MONITOR_FIRSTLVL_DIR)
except Exception as e:
logger.warn(f"Failed to listdir of {MONITOR_FIRSTLVL_DIR}: {str(e)}")
general_logger.warn(f"Failed to listdir of {MONITOR_FIRSTLVL_DIR}: {str(e)}")
return

for dir in dirs:
Expand All @@ -37,7 +37,7 @@ def timed_du():
size_b, *_ = result.stdout.split("\t")
Singleton.cached_du[dir] = int(size_b)
except Exception as e:
logger.warn(f"Failed to check du of {str(path_to_dir)}: {str(e)}")
general_logger.warn(f"Failed to check du of {str(path_to_dir)}: {str(e)}")


def on_startup():
Expand Down
2 changes: 1 addition & 1 deletion api/server/volumes/parcellationmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from api.models.volumes.parcellationmap import MapModel
from api.models.volumes.volume import MapType
from api.models._commons import DataFrameModel
from api.common import router_decorator, get_filename, logger, NotFound
from api.common import router_decorator, get_filename, NotFound
from api.common.data_handlers.volumes.parcellationmap import get_map, get_region_statistic_map, get_region_statistic_map_info, get_parcellation_labelled_map, assign_point, get_resampled_map
from api.server.util import SapiCustomRoute
import os
Expand Down

0 comments on commit 11ccb46

Please sign in to comment.