Skip to main content

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

Latest Update:

  • Turn ON/OFF console log
  • Provide Custom name to the logger.
logger = LoggerSingleton.get_logger(config, log_console=True, logger_name="app-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
    "debug": True                         # Control whether debug logs are flushed (default: True)
}

🔍 Debug Log Filtering

The debug flag controls whether logger.debug(...) statements are flushed to the database or fallback file:

  • ✅ When debug=True (default), all log levels (DEBUG, INFO, WARNING, etc.) are processed.
  • 🚫 When debug=False, DEBUG-level logs are suppressed from being written to storage.

This helps reduce clutter in production environments while keeping verbose logs during development.


🧠 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...")  # Will be skipped if debug=False
    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.")  # Will be skipped if debug=False
    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",
    "debug": False
}

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.")  # Suppressed due to debug=False
    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.")  # Suppressed if debug=False

✅ Best Practices (Do's)

  • ✅ Use LoggerSingleton.get_logger(config) once per mode/config.
  • ✅ Always use context loggers (conversation_logger or training_logger) for automatic flushing and tagging.
  • ✅ Call LoggerSingleton.close_logger() before switching modes.
  • ✅ Use .reopen() when recovering from DB crashes mid-run.
  • ✅ Toggle the debug flag for verbosity control in different environments.

❌ 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


Let me know if you'd also like a new section added for **custom filters or extensions**.

Metadata

Release files for rag-mongo-logger 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rag-mongo-logger 0.1.4
File Size Uploaded Provenance Actions
rag_mongo_logger-0.1.4.tar.gz 11.4 kB Without provenance Details for rag_mongo_logger-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rag-mongo-logger 0.1.4
File Interpreter ABI Platform Actions
rag_mongo_logger-0.1.4-py3-none-any.whl Python 3 none any Details for rag_mongo_logger-0.1.4-py3-none-any.whl

Total release size: 21.8 kB

Release files / rag_mongo_logger-0.1.4.tar.gz

Download URL rag_mongo_logger-0.1.4.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
04999a7e48e05ead48fc81c17adfc5afac6aaebc2bba72632c80aa98470b98e9
BLAKE2b-256 checksum
How to use checksums
fcfe72fc2ef4401e57979047687ad996de4cc6928211b28d79f40f84f903a409
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / rag_mongo_logger-0.1.4-py3-none-any.whl

Download URL rag_mongo_logger-0.1.4-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
54bc0f1d4c46bef30e1af79a675ddd2fcca19e3b871bd34735863cff85a340a8
BLAKE2b-256 checksum
How to use checksums
6b39158de0e9ae2d4b4d35c70f122412c9e61dd08223f480c3e727b12e568a3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Security for rag-mongo-logger 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page