From 14b1eb54ccd5819eed82a311dbe3d2580db3c8b0 Mon Sep 17 00:00:00 2001 From: Canicula98 Date: Mon, 21 Oct 2024 16:20:18 +0200 Subject: [PATCH 1/3] client config logger --- examples/client-logger-config/client.py | 27 +++++++++++++ .../client-logger-config/docker-compose.yaml | 40 +++++++++++++++++++ .../model/client_model.py | 18 +++++++++ src/dataclay/client/api.py | 3 ++ src/dataclay/config.py | 6 ++- 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 examples/client-logger-config/client.py create mode 100644 examples/client-logger-config/docker-compose.yaml create mode 100644 examples/client-logger-config/model/client_model.py diff --git a/examples/client-logger-config/client.py b/examples/client-logger-config/client.py new file mode 100644 index 0000000..6578f78 --- /dev/null +++ b/examples/client-logger-config/client.py @@ -0,0 +1,27 @@ +import logging +import os + +from model.client_model import Client_model + +from dataclay import Client + +logger = logging.getLogger(__name__) + +logger.debug("First Debug log") +logger.info("First Info log") + +logging.basicConfig(force=True, level=getattr(logging, os.getenv("LOG_LEVEL", "DEBUG").upper())) +dclogger = logging.getLogger("dataclay") +dclogger.setLevel(logging.INFO) +grpclogger = logging.getLogger("grpc") +grpclogger.setLevel(logging.INFO) + +client = Client(host="127.0.0.1", username="testuser", password="s3cret", dataset="testdata") +client.start() + +client_mod = Client_model("Client") +client_mod.make_persistent("alias") + +name = client_mod.get_name() +logger.debug("My name is : %s", name) +logger.info("My name is : %s", name) diff --git a/examples/client-logger-config/docker-compose.yaml b/examples/client-logger-config/docker-compose.yaml new file mode 100644 index 0000000..346969f --- /dev/null +++ b/examples/client-logger-config/docker-compose.yaml @@ -0,0 +1,40 @@ +services: + + redis: + image: redis:latest + ports: + - 6379:6379 + + metadata-service: + image: "ghcr.io/bsc-dom/dataclay:dev" + depends_on: + - redis + ports: + - 16587:16587 + environment: + - DATACLAY_KV_HOST=redis + - DATACLAY_KV_PORT=6379 + - DATACLAY_ID + - DATACLAY_PASSWORD=s3cret + - DATACLAY_USERNAME=testuser + - DATACLAY_DATASET=testdata + - DATACLAY_METADATA_PORT=16587 + - DATACLAY_LOGLEVEL=DEBUG + command: python -m dataclay.metadata + + backend: + image: "ghcr.io/bsc-dom/dataclay:dev" + depends_on: + - redis + ports: + - 6867:6867 + environment: + - DATACLAY_KV_HOST=redis + - DATACLAY_KV_PORT=6379 + - DATACLAY_BACKEND_ID + - DATACLAY_BACKEND_NAME + - DATACLAY_BACKEND_PORT=6867 + - DATACLAY_LOGLEVEL=DEBUG + command: python -m dataclay.backend + volumes: + - ./model:/workdir/model:ro \ No newline at end of file diff --git a/examples/client-logger-config/model/client_model.py b/examples/client-logger-config/model/client_model.py new file mode 100644 index 0000000..c37f28a --- /dev/null +++ b/examples/client-logger-config/model/client_model.py @@ -0,0 +1,18 @@ +from dataclay import DataClayObject, activemethod + + +class Client_model(DataClayObject): + + name: str + + @activemethod + def __init__(self, name): + self.name = name + + @activemethod + def change_name(self, name): + self.name = name + + @activemethod + def get_name(self): + return self.name diff --git a/src/dataclay/client/api.py b/src/dataclay/client/api.py index ff21246..42794a3 100644 --- a/src/dataclay/client/api.py +++ b/src/dataclay/client/api.py @@ -18,6 +18,7 @@ from dataclay.config import ( ClientSettings, get_runtime, + loggerConfig, session_var, set_runtime, settings, @@ -169,6 +170,8 @@ def __init__( def start(self): """Start the client runtime""" + loggerConfig(level=settings.loglevel) + if self.is_active: logger.warning("Client already active. Ignoring") return diff --git a/src/dataclay/config.py b/src/dataclay/config.py index e08badd..9656df3 100644 --- a/src/dataclay/config.py +++ b/src/dataclay/config.py @@ -232,4 +232,8 @@ def set_runtime(new_runtime: Union[ClientRuntime, BackendRuntime]): current_runtime = new_runtime -logging.basicConfig(level=settings.loglevel) +def loggerConfig(**kwargs): + logging.basicConfig(**kwargs) + + +loggerConfig(level=settings.loglevel) From 8f6952a31fae86b68e5e48a1b4f886adae334e77 Mon Sep 17 00:00:00 2001 From: Canicula98 Date: Wed, 13 Nov 2024 18:14:56 +0100 Subject: [PATCH 2/3] logging docu & logger_config() rename --- docs/index.rst | 1 + docs/logging.rst | 43 ++++++++++++++++++++++++++++++++++++++ src/dataclay/client/api.py | 4 ++-- src/dataclay/config.py | 4 ++-- 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 docs/logging.rst diff --git a/docs/index.rst b/docs/index.rst index 7a407c2..923db6d 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -12,6 +12,7 @@ dataClay advanced-usage telemetry hpc-tracing + logging examples/index .. toctree:: diff --git a/docs/logging.rst b/docs/logging.rst new file mode 100644 index 0000000..049fc5e --- /dev/null +++ b/docs/logging.rst @@ -0,0 +1,43 @@ +======= +Logging +======= + +DataClay offers logging to allow code debugging and information collection. + +When dataclay is imported, the logging is first initialized with a basic configuration in the config.py file: + +.. code-block:: python + :caption: config.py + + ... + + class Settings(BaseSettings): + + ... + + loglevel: Annotated[str, StringConstraints(strip_whitespace=True, to_upper=True)] = "INFO" + + ... + + settings = Settings() + + ... + + + def logger_config(**kwargs): + logging.basicConfig(**kwargs) + + + logger_config(level=settings.loglevel) + + + +If the user wants to configure its own logging, it can be done by importing the logging library and modifying +the basicConfig. When client.start() function is called, then logger_config() is executed again, and if the argument +"force" is True then the logging configuration is overwritten. + +.. warning:: + When modifying the basicConfig remember that the **force=True** parameter is mandatory. Otherwise, this new + configuration will be obviated. + +An example is available in `GitHub `_ diff --git a/src/dataclay/client/api.py b/src/dataclay/client/api.py index 42794a3..6497166 100644 --- a/src/dataclay/client/api.py +++ b/src/dataclay/client/api.py @@ -18,7 +18,7 @@ from dataclay.config import ( ClientSettings, get_runtime, - loggerConfig, + logger_config, session_var, set_runtime, settings, @@ -170,7 +170,7 @@ def __init__( def start(self): """Start the client runtime""" - loggerConfig(level=settings.loglevel) + logger_config(level=settings.loglevel) if self.is_active: logger.warning("Client already active. Ignoring") diff --git a/src/dataclay/config.py b/src/dataclay/config.py index 9656df3..c231fc5 100644 --- a/src/dataclay/config.py +++ b/src/dataclay/config.py @@ -232,8 +232,8 @@ def set_runtime(new_runtime: Union[ClientRuntime, BackendRuntime]): current_runtime = new_runtime -def loggerConfig(**kwargs): +def logger_config(**kwargs): logging.basicConfig(**kwargs) -loggerConfig(level=settings.loglevel) +logger_config(level=settings.loglevel) From 872ecdeb02917d255d8b76a6a126a2b167be30b9 Mon Sep 17 00:00:00 2001 From: Canicula98 Date: Wed, 13 Nov 2024 18:23:41 +0100 Subject: [PATCH 3/3] PascalCase fix --- examples/client-logger-config/client.py | 4 ++-- examples/client-logger-config/model/client_model.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/client-logger-config/client.py b/examples/client-logger-config/client.py index 6578f78..5639989 100644 --- a/examples/client-logger-config/client.py +++ b/examples/client-logger-config/client.py @@ -1,7 +1,7 @@ import logging import os -from model.client_model import Client_model +from model.client_model import ClientModel from dataclay import Client @@ -19,7 +19,7 @@ client = Client(host="127.0.0.1", username="testuser", password="s3cret", dataset="testdata") client.start() -client_mod = Client_model("Client") +client_mod = ClientModel("Client") client_mod.make_persistent("alias") name = client_mod.get_name() diff --git a/examples/client-logger-config/model/client_model.py b/examples/client-logger-config/model/client_model.py index c37f28a..ef88b0f 100644 --- a/examples/client-logger-config/model/client_model.py +++ b/examples/client-logger-config/model/client_model.py @@ -1,7 +1,7 @@ from dataclay import DataClayObject, activemethod -class Client_model(DataClayObject): +class ClientModel(DataClayObject): name: str