Skip to content

Commit

Permalink
Merge pull request #48 from bsc-dom/logger-config
Browse files Browse the repository at this point in the history
client config logger
  • Loading branch information
alexbarcelo authored Nov 18, 2024
2 parents 4159431 + f080791 commit cf35736
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dataClay
main-concepts
alien-objects
advanced-usage
logging
examples/index

.. toctree::
Expand Down
43 changes: 43 additions & 0 deletions docs/logging.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/bsc-dom/dataclay/tree/main/examples/client-logger-config>`_
27 changes: 27 additions & 0 deletions examples/client-logger-config/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import logging
import os

from model.client_model import ClientModel

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 = ClientModel("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)
40 changes: 40 additions & 0 deletions examples/client-logger-config/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions examples/client-logger-config/model/client_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from dataclay import DataClayObject, activemethod


class ClientModel(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
3 changes: 3 additions & 0 deletions src/dataclay/client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from dataclay.config import (
ClientSettings,
get_runtime,
logger_config,
session_var,
set_runtime,
settings,
Expand Down Expand Up @@ -172,6 +173,8 @@ def __init__(
def start(self):
"""Start the client runtime"""

logger_config(level=settings.loglevel)

if self.is_active:
logger.warning("Client already active. Ignoring")
return
Expand Down
6 changes: 5 additions & 1 deletion src/dataclay/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,8 @@ def set_runtime(new_runtime: Union[ClientRuntime, BackendRuntime]):
current_runtime = new_runtime


logging.basicConfig(level=settings.loglevel)
def logger_config(**kwargs):
logging.basicConfig(**kwargs)


logger_config(level=settings.loglevel)

0 comments on commit cf35736

Please sign in to comment.