Async buffered logger with MongoDB and PostgreSQL support for your RAG applications
Project description
rag-mongo-logger
🚀 A robust, async-buffered Python logging library supporting MongoDB, PostgreSQL, and local file fallback. Designed for chatbots, training pipelines, or any system needing conversation-oriented logging.
📦 Installation
Install from PyPI:
pip install rag-mongo-logger
⚙️ Configuration
config = {
"uri": "mongodb://localhost:27017/", # or PostgreSQL URI
"db": "conversational_logs", # MongoDB database name
"env": "prod", # Optional: "dev", "staging", "prod"
"logger_mode": "chat", # "chat" or "training"
"log_batch_size": 5, # Buffer size before flushing
"log_fallback_file": "fallback.txt" # Local file fallback on DB failure
}
🧠 Modes of Operation
1. Chat Mode (logger_mode='chat')
Logs conversations with optional metadata like conversation_id, bot_id, user_id.
from rag_mongo_logger.singleton import LoggerSingleton
from rag_mongo_logger.context_logging import conversation_logger
logger = LoggerSingleton.get_logger(config)
with conversation_logger(logger, conversation_id="conv-123", bot_id="bot-xyz", user_id="admin_001") as log:
log.info("User started the conversation.")
log.debug("Fetching documents...")
log.warning("Timeout fetching from external API.")
2. Training Mode (logger_mode='training')
Logs training operations with context like bot_id and training_id.
from rag_mongo_logger.context_logging import training_logger
with training_logger(logger, bot_id="bot-abc", training_id="train-001") as log:
log.info("Training session started.")
log.debug("Processing document 3 of 10.")
log.warning("Skipped document due to format issue.")
🔁 Manual DB Reconnect
If your DB (Mongo/PostgreSQL) crashes and comes back up mid-execution:
logger.reopen()
Or expose a FastAPI endpoint:
@app.post("/logger/reopen")
def trigger_reconnect():
logger.reopen()
return {"status": "Reconnection attempted"}
🧪 Example Usage
from rag_mongo_logger.singleton import LoggerSingleton
from rag_mongo_logger.context_logging import conversation_logger, training_logger
config = {
"uri": "mongodb://localhost:27017/",
"db": "conversational_logs",
"env": "prod",
"logger_mode": "training",
"log_batch_size": 5,
"log_fallback_file": "app_fallback_logs.txt"
}
logger = LoggerSingleton.get_logger(config)
# Training mode
with training_logger(logger, bot_id="bot_v2", training_id="train_123") as log:
log.info("Training started.")
log.debug("Document 1 processed.")
log.warning("Skipped doc due to malformed content.")
# Switch to chat mode
LoggerSingleton.close_logger()
config["logger_mode"] = "chat"
LoggerSingleton.reopen()
logger = LoggerSingleton.get_logger(config)
# Chat mode
with conversation_logger(logger, conversation_id="conv_42", bot_id="bot_v2", user_id="admin") as log:
log.info("Conversation started.")
log.debug("User asked about refund policy.")
✅ Best Practices (Do's)
- ✅ Use
LoggerSingleton.get_logger(config)once per mode/config. - ✅ Always use context loggers (
conversation_loggerortraining_logger) for automatic flushing and tagging. - ✅ Call
LoggerSingleton.close_logger()before switching modes. - ✅ Use
.reopen()when recovering from DB crashes mid-run. - ✅ Provide consistent
conversation_id,bot_id,user_idfor grouping logs.
❌ Pitfalls to Avoid (Don'ts)
- ❌ Don't call
get_logger()repeatedly without closing — it’s a singleton. - ❌ Don't log outside the context manager unless necessary — flushing won't be guaranteed.
- ❌ Don't forget to close the logger at the end of scripts (
LoggerSingleton.close_logger()). - ❌ Don't rely solely on the DB connection — always configure a fallback file.
📁 Fallback Logs
If the DB is down, logs are written to log_fallback_file (e.g., fallback.txt), using a rotating file handler (5MB x 3 backups).
📜 License
MIT License © Siddhesh Dosi
🙋 Need Help?
Open an issue or contribute at GitHub Repository
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rag_mongo_logger-0.1.2.tar.gz.
File metadata
- Download URL: rag_mongo_logger-0.1.2.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abfbdf1a408772874777e97eb2923d79aceedb088fd4ccaa8210f2c27c810bbc
|
|
| MD5 |
c26e840f09fbccd35017dbf80b6bead4
|
|
| BLAKE2b-256 |
3608efe4aa821f6b314573aa8d97598387c1c3d7f15dc5b18f3ab108e49d3991
|
File details
Details for the file rag_mongo_logger-0.1.2-py3-none-any.whl.
File metadata
- Download URL: rag_mongo_logger-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9ea186b3fb7501f157d7da2cce9a24ec1a73e038ceed8a9214859d2eafabfab
|
|
| MD5 |
1cc7d652f4f9137960d1a30b158b33c4
|
|
| BLAKE2b-256 |
d27682e0e6ae7bb7560b3918e739c742d4877938e7cb37918caaa2c77961fd25
|