diff --git a/services/search/analytics.py b/services/search/analytics.py index 64e61e962..b5602bae4 100644 --- a/services/search/analytics.py +++ b/services/search/analytics.py @@ -6,21 +6,29 @@ from collections import Counter from pathlib import Path from typing import Dict, Any +from core.constants import DATA_DIR + from .cache import cache_metrics logger = logging.getLogger(__name__) -# Dedicated error logger with file handler -_error_log_path = Path(__file__).resolve().parent.parent / "search_engine_error.log" -_error_handler = logging.FileHandler(_error_log_path, encoding="utf-8") -_error_handler.setLevel(logging.WARNING) -_error_handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s %(message)s")) +# Dedicated error logger — write to the data logs directory (writable on both +# native runs and Docker, where DATA_DIR resolves to the bind-mounted volume). +_log_dir = Path(DATA_DIR) / "logs" +_error_log_path = _log_dir / "search_engine_error.log" error_logger = logging.getLogger("search_engine_error") -error_logger.addHandler(_error_handler) error_logger.propagate = False +try: + _log_dir.mkdir(parents=True, exist_ok=True) + _error_handler = logging.FileHandler(_error_log_path, encoding="utf-8") + _error_handler.setLevel(logging.WARNING) + _error_handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s %(message)s")) + error_logger.addHandler(_error_handler) +except Exception as _e: + logging.getLogger(__name__).warning("search_engine_error log handler unavailable: %s", _e) -# Analytics file -ANALYTICS_FILE = Path(__file__).resolve().parent.parent / "search_analytics.json" +# Analytics file — also in the writable logs volume. +ANALYTICS_FILE = _log_dir / "search_analytics.json" # ----------------------------------------------------------------------