99from loguru import logger
1010from unidecode import unidecode
1111
12+ import basic_memory
1213from basic_memory .config import config
1314
15+ import logfire
16+
1417
1518def generate_permalink (file_path : Union [Path , str ]) -> str :
1619 """Generate a stable permalink from a file path.
@@ -61,19 +64,45 @@ def generate_permalink(file_path: Union[Path, str]) -> str:
6164 return "/" .join (clean_segments )
6265
6366
64- def setup_logging (home_dir : Path = config .home , log_file : Optional [str ] = None ) -> None :
67+ def setup_logging (
68+ home_dir : Path = config .home , log_file : Optional [str ] = None
69+ ) -> None : # pragma: no cover
6570 """
6671 Configure logging for the application.
72+ :param home_dir: the root directory for the application
73+ :param log_file: the name of the log file to write to
74+ :param app: the fastapi application instance
6775 """
6876
6977 # Remove default handler and any existing handlers
7078 logger .remove ()
7179
72- # Add file handler
73- if log_file :
80+ # Add file handler if we are not running tests
81+ if log_file and config .env != "test" :
82+ # enable pydantic logfire
83+ logfire .configure (
84+ code_source = logfire .CodeSource (
85+ repository = "https://github.com/basicmachines-co/basic-memory" ,
86+ revision = basic_memory .__version__ ,
87+ root_path = "/src/basic_memory" ,
88+ ),
89+ environment = config .env ,
90+ )
91+ logger .configure (handlers = [logfire .loguru_handler ()])
92+
93+ # instrument code spans
94+ logfire .instrument_sqlite3 ()
95+ logfire .instrument_pydantic ()
96+
97+ from basic_memory .db import _engine as engine
98+
99+ if engine :
100+ logfire .instrument_sqlalchemy (engine = engine )
101+
102+ # setup logger
74103 log_path = home_dir / log_file
75104 logger .add (
76- str (log_path ), # loguru expects a string path
105+ str (log_path ),
77106 level = config .log_level ,
78107 rotation = "100 MB" ,
79108 retention = "10 days" ,
@@ -85,3 +114,5 @@ def setup_logging(home_dir: Path = config.home, log_file: Optional[str] = None)
85114
86115 # Add stderr handler
87116 logger .add (sys .stderr , level = config .log_level , backtrace = True , diagnose = True , colorize = True )
117+
118+ logger .info (f"ENV: '{ config .env } ' Log level: '{ config .log_level } ' Logging to { log_file } " )
0 commit comments