Refactor LoggingConfig methods for enabling and disabling logging

This commit is contained in:
2024-11-01 08:39:14 -04:00
parent cd0be3ad89
commit 173162e798
3 changed files with 14 additions and 6 deletions
+6
View File
@@ -113,6 +113,11 @@ def generate_text(
return provider.generate_text(prompt=prompt, llm_model=llm_model, **kwargs)
def enable_logfire() -> None:
"""Enable logfire logging."""
settings.logging.enable_logfire()
# Syntax sugar.
Plugin = BasePlugin
@@ -125,4 +130,5 @@ __all__ = [
"BasePlugin",
"Session",
"Plugin",
"enable_logfire",
]
+4 -2
View File
@@ -7,10 +7,12 @@ from .settings import settings
def logger(func: Callable[..., Any]) -> Callable[..., Any]:
"""A @logger decorator that logs the function parameters, function returns, and exceptions raised if logging is enabled."""
"""A decorator that logs the function parameters, function returns,
and exceptions raised if logging is enabled, using logfire.
"""
def wrapper(*args, **kwargs) -> Any:
if not settings.logging.enabled:
if not settings.logging.is_enabled:
return func(*args, **kwargs)
logfire.info(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}")
t1 = time.perf_counter()
+4 -4
View File
@@ -7,7 +7,7 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
class LoggingConfig(BaseSettings):
"""The class that holds all the logging settings for the application."""
enabled: bool = Field(False, description="Enable logging")
is_enabled: bool = Field(False, description="Enable logging")
model_config = SettingsConfigDict(extra="forbid")
@@ -22,7 +22,7 @@ class LoggingConfig(BaseSettings):
"To enable logging, please install logfire: `pip install logfire`"
) from e
self.enabled = True
self.is_enabled = True
logfire.configure(**kwargs)
basicConfig(handlers=[logfire.LogfireLoggingHandler()])
@@ -30,12 +30,12 @@ class LoggingConfig(BaseSettings):
logfire.configure(**kwargs)
basicConfig(handlers=[logfire.LogfireLoggingHandler()])
except Exception as e:
self.enabled = False # Reset flag on failure
self.is_enabled = False # Reset flag on failure
raise RuntimeError("Failed to configure logging") from e
def disable_logfire(self) -> None:
"""Disable logging for the application."""
self.enabled = False
self.is_enabled = False
class Settings(BaseSettings):