From b5b150a773c248f8a4d86bad3d2a43272023f024 Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Date: Fri, 24 Jul 2026 04:42:50 +0500 Subject: [PATCH] fix: replace print() with logger in encryption.py and classic_rag.py (#2565) * fix: replace print() with logger in classic_rag.py and encryption.py Three print() calls bypassed the structured logging system, risking exposure of rephrased user queries and credential error details to unmanaged stdout. Replace with logger.debug() / logger.warning() and add a module-level logger to both files. Also migrate all bare logging.xxx() calls in classic_rag.py to the module-level logger for consistency. * (chore:encryption.py) ruff --------- Co-authored-by: ManishMadan2882 --- application/retriever/classic_rag.py | 20 +++++++++++--------- application/security/encryption.py | 7 +++++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/application/retriever/classic_rag.py b/application/retriever/classic_rag.py index dbf395c6..b343e1ea 100644 --- a/application/retriever/classic_rag.py +++ b/application/retriever/classic_rag.py @@ -7,6 +7,8 @@ from application.retriever.labels import labels_from_metadata from application.utils import num_tokens_from_string from application.vectorstore.vector_creator import VectorCreator +logger = logging.getLogger(__name__) + class ClassicRAG(BaseRetriever): # The group's real top-k, set by the Dispatcher when it inflates ``chunks`` @@ -39,14 +41,14 @@ class ClassicRAG(BaseRetriever): try: self.chunks = int(chunks) except ValueError: - logging.warning( + logger.warning( f"Invalid chunks value '{chunks}', using default value 2" ) self.chunks = 2 else: self.chunks = chunks user_id = decoded_token.get("sub") if decoded_token else "default" - logging.info( + logger.info( f"ClassicRAG initialized with chunks={self.chunks}, user_id={user_id}, " f"sources={'active_docs' in source and source['active_docs'] is not None}" ) @@ -105,13 +107,13 @@ class ClassicRAG(BaseRetriever): def _validate_vectorstore_config(self): """Validate vectorstore IDs and remove any empty/invalid entries""" if not self.vectorstores: - logging.warning("No vectorstores configured for retrieval") + logger.warning("No vectorstores configured for retrieval") return invalid_ids = [ vs_id for vs_id in self.vectorstores if not vs_id or not vs_id.strip() ] if invalid_ids: - logging.warning(f"Found invalid vectorstore IDs: {invalid_ids}") + logger.warning(f"Found invalid vectorstore IDs: {invalid_ids}") self.vectorstores = [ vs_id for vs_id in self.vectorstores if vs_id and vs_id.strip() ] @@ -144,10 +146,10 @@ class ClassicRAG(BaseRetriever): model=getattr(self.llm, "model_id", None) or self.model_id, messages=messages, ) - print(f"Rephrased query: {rephrased_query}") + logger.debug(f"Rephrased query: {rephrased_query}") return rephrased_query if rephrased_query else self.original_question except Exception as e: - logging.error(f"Error rephrasing query: {e}", exc_info=True) + logger.error(f"Error rephrasing query: {e}", exc_info=True) return self.original_question def _fetch_candidates(self, docsearch, question, src_k, score_threshold): @@ -175,7 +177,7 @@ class ClassicRAG(BaseRetriever): def _get_data(self): if self.chunks == 0 or not self.vectorstores: - logging.info( + logger.info( f"ClassicRAG._get_data: Skipping retrieval - chunks={self.chunks}, " f"vectorstores_count={len(self.vectorstores) if self.vectorstores else 0}" ) @@ -277,13 +279,13 @@ class ClassicRAG(BaseRetriever): break except Exception as e: - logging.error( + logger.error( f"Error searching vectorstore {vectorstore_id}: {e}", exc_info=True, ) continue - logging.info( + logger.info( f"ClassicRAG._get_data: Retrieval complete - retrieved {len(all_docs)} documents " f"(requested chunks={self.chunks}, chunks_per_source={chunks_per_source}, " f"cumulative_tokens={cumulative_tokens}/{token_budget})" diff --git a/application/security/encryption.py b/application/security/encryption.py index 4cb3a4d5..f654d6bf 100644 --- a/application/security/encryption.py +++ b/application/security/encryption.py @@ -1,5 +1,6 @@ import base64 import json +import logging import os from cryptography.hazmat.backends import default_backend @@ -9,6 +10,8 @@ from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC from application.core.settings import settings +logger = logging.getLogger(__name__) + def _derive_key(user_id: str, salt: bytes) -> bytes: app_secret = settings.ENCRYPTION_SECRET_KEY @@ -45,7 +48,7 @@ def encrypt_credentials(credentials: dict, user_id: str) -> str: result = salt + iv + encrypted_data return base64.b64encode(result).decode() except Exception as e: - print(f"Warning: Failed to encrypt credentials: {e}") + logger.warning(f"Failed to encrypt credentials: {e}") return "" @@ -69,7 +72,7 @@ def decrypt_credentials(encrypted_data: str, user_id: str) -> dict: return json.loads(decrypted_data.decode()) except Exception as e: - print(f"Warning: Failed to decrypt credentials: {e}") + logger.warning(f"Failed to decrypt credentials: {e}") return {}