From 56b1e65d70b355c67ea0ba1a8e1ca542c2e25588 Mon Sep 17 00:00:00 2001 From: Siddhesh Agarwal Date: Fri, 1 Nov 2024 13:06:06 +0530 Subject: [PATCH] moved logging functions to LoggingConfig from Settings --- simplemind/settings.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/simplemind/settings.py b/simplemind/settings.py index a0dad2f..e5bb822 100644 --- a/simplemind/settings.py +++ b/simplemind/settings.py @@ -1,4 +1,4 @@ -from typing import Literal, Optional, Union +from typing import Literal, Optional, Union, Unpack from pydantic import Field, SecretStr, field_validator from pydantic_settings import BaseSettings, SettingsConfigDict @@ -10,10 +10,28 @@ class LoggingConfig(BaseSettings): """The class that holds all the logging settings for the application.""" enabled: bool = Field(False, description="Enable logging") - level: logging_level = Field("INFO", description="The logging level") model_config = SettingsConfigDict(extra="forbid") + def enable_logging(self, **kwargs) -> None: + """Enable logging for the application.""" + # adding imports here to avoid forced dependencies + try: + import logfire + except ImportError as e: + raise ImportError( + "To enable logging, please install logfire: `pip install logfire`" + ) from e + from logging import basicConfig + + self.enabled = True + logfire.configure(**kwargs) + basicConfig(handlers=[logfire.LogfireLoggingHandler()]) + + def disable_logging(self) -> None: + """Disable logging for the application.""" + self.enabled = False + class Settings(BaseSettings): """The class that holds all the API keys for the application.""" @@ -51,19 +69,5 @@ class Settings(BaseSettings): key = getattr(self, f"{provider.upper()}_API_KEY", None) return key.get_secret_value() if key else None - def enable_logging(self) -> None: - """Enable logging for the application.""" - # adding imports here to avoid forced dependencies - import logfire - from logging import basicConfig - - self.logging.enabled = True - logfire.configure() - basicConfig(handlers=[logfire.LogfireLoggingHandler()]) - - def disable_logging(self) -> None: - """Disable logging for the application.""" - self.logging.enabled = False - settings = Settings()