feat: configure logfire telemetry (#12)

Co-authored-by: phernandez <phernandez@basicmachines.co>
This commit is contained in:
Paul Hernandez
2025-02-18 15:53:38 -06:00
committed by GitHub
parent a6b46908b1
commit 6da143898b
10 changed files with 487 additions and 14 deletions
+35 -4
View File
@@ -9,8 +9,11 @@ from typing import Optional, Union
from loguru import logger
from unidecode import unidecode
import basic_memory
from basic_memory.config import config
import logfire
def generate_permalink(file_path: Union[Path, str]) -> str:
"""Generate a stable permalink from a file path.
@@ -61,19 +64,45 @@ def generate_permalink(file_path: Union[Path, str]) -> str:
return "/".join(clean_segments)
def setup_logging(home_dir: Path = config.home, log_file: Optional[str] = None) -> None:
def setup_logging(
home_dir: Path = config.home, log_file: Optional[str] = None
) -> None: # pragma: no cover
"""
Configure logging for the application.
:param home_dir: the root directory for the application
:param log_file: the name of the log file to write to
:param app: the fastapi application instance
"""
# Remove default handler and any existing handlers
logger.remove()
# Add file handler
if log_file:
# Add file handler if we are not running tests
if log_file and config.env != "test":
# enable pydantic logfire
logfire.configure(
code_source=logfire.CodeSource(
repository="https://github.com/basicmachines-co/basic-memory",
revision=basic_memory.__version__,
root_path="/src/basic_memory",
),
environment=config.env,
)
logger.configure(handlers=[logfire.loguru_handler()])
# instrument code spans
logfire.instrument_sqlite3()
logfire.instrument_pydantic()
from basic_memory.db import _engine as engine
if engine:
logfire.instrument_sqlalchemy(engine=engine)
# setup logger
log_path = home_dir / log_file
logger.add(
str(log_path), # loguru expects a string path
str(log_path),
level=config.log_level,
rotation="100 MB",
retention="10 days",
@@ -85,3 +114,5 @@ def setup_logging(home_dir: Path = config.home, log_file: Optional[str] = None)
# Add stderr handler
logger.add(sys.stderr, level=config.log_level, backtrace=True, diagnose=True, colorize=True)
logger.info(f"ENV: '{config.env}' Log level: '{config.log_level}' Logging to {log_file}")