From e2a016a9c2e06e3aea28b3778cb26333b56c9bf2 Mon Sep 17 00:00:00 2001 From: RINO-GAELICO Date: Wed, 9 Oct 2024 14:37:53 -0700 Subject: [PATCH 1/2] Added function to allow dynamic logging configuration using a provided dictionary. --- healthchain/decorators.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/healthchain/decorators.py b/healthchain/decorators.py index 9376ae6..dede97f 100644 --- a/healthchain/decorators.py +++ b/healthchain/decorators.py @@ -1,4 +1,5 @@ import logging +import logging.config import threading import asyncio import json @@ -151,6 +152,43 @@ def __init__(self) -> None: return sandbox_decorator(service_config) +def configure_logging(logging_config=None): + """ + Configures logging based on the provided configuration. If no configuration is provided, + it defaults to a basic configuration with INFO level logging. + + Parameters: + logging_config (dict, optional): A dictionary containing configurations for logging. + The dictionary should follow the structure defined in the Python logging configuration schema. + + Returns: + None + + Example: + configure_logging(logging_config={ + "version": 1, + "formatters": { + "detailed": { + "class": "logging.Formatter", + "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + } + }, + "handlers": { + "console": { + "class": "logging.StreamHandler", + "formatter": "detailed" + } + }, + "root": { + "handlers": ["console"], + "level": "DEBUG", + }, + }) + """ + if logging_config: + logging.config.dictConfig(logging_config) + else: + logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') def sandbox_decorator(service_config: Optional[Dict] = None) -> Callable: """ @@ -210,6 +248,7 @@ def new_init(self, *args: Any, **kwargs: Any) -> None: # Set the new init cls.__init__ = new_init + def start_sandbox( self, service_id: str = "1", From acdf5c07b0651e5cb75a73b9293cd588b0ddc5b8 Mon Sep 17 00:00:00 2001 From: RINO-GAELICO Date: Mon, 14 Oct 2024 18:00:37 -0700 Subject: [PATCH 2/2] Removed the function configure_logging and added logging_config as argument to start_sandbox --- healthchain/decorators.py | 50 ++++++++++----------------------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/healthchain/decorators.py b/healthchain/decorators.py index dede97f..d1c59e2 100644 --- a/healthchain/decorators.py +++ b/healthchain/decorators.py @@ -152,43 +152,6 @@ def __init__(self) -> None: return sandbox_decorator(service_config) -def configure_logging(logging_config=None): - """ - Configures logging based on the provided configuration. If no configuration is provided, - it defaults to a basic configuration with INFO level logging. - - Parameters: - logging_config (dict, optional): A dictionary containing configurations for logging. - The dictionary should follow the structure defined in the Python logging configuration schema. - - Returns: - None - - Example: - configure_logging(logging_config={ - "version": 1, - "formatters": { - "detailed": { - "class": "logging.Formatter", - "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" - } - }, - "handlers": { - "console": { - "class": "logging.StreamHandler", - "formatter": "detailed" - } - }, - "root": { - "handlers": ["console"], - "level": "DEBUG", - }, - }) - """ - if logging_config: - logging.config.dictConfig(logging_config) - else: - logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') def sandbox_decorator(service_config: Optional[Dict] = None) -> Callable: """ @@ -248,12 +211,12 @@ def new_init(self, *args: Any, **kwargs: Any) -> None: # Set the new init cls.__init__ = new_init - def start_sandbox( self, service_id: str = "1", save_data: bool = True, save_dir: str = "./output/", + logging_config: Optional[Dict] = None, ) -> None: """ Starts the sandbox: initialises service and sends a request through the client. @@ -268,6 +231,17 @@ def start_sandbox( self.sandbox_id = uuid.uuid4() + if logging_config: + logging.config.dictConfig(logging_config) + else: + # Set up default logging configuration + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + ) + + log = logging.getLogger(__name__) + # Start service on thread log.info( f"Starting sandbox {self.sandbox_id} with {self.__class__.__name__} of type {self.type.value}..."