Skip to content

MongoDB

MongoHandler inserts log entries into a MongoDB collection using pymongo.

Installation

This integration requires the pymongo package.

bash
uv add logly[mongodb]
bash
pip install "logly[mongodb]"
bash
uv add pymongo
bash
pip install pymongo

Missing Dependency

If pymongo is not installed, you'll see:

ModuleNotFoundError: No module named 'pymongo'

Usage

python
from logly import logger
from logly.integrations.mongodb import MongoHandler

handler = MongoHandler(
    "mongodb://localhost:27017",
    database="logs",
    collection="app_logs",
)
logger.add(handler, level="WARNING")

Constructor Args

ArgumentTypeDefaultDescription
uristrmongodb://localhost:27017MongoDB connection URI
databasestrloglyDatabase name
collectionstrlogsCollection name
timeoutfloat5.0Socket timeout in seconds

Tips

  • Create an index on the timestamp field for efficient querying.
  • Use a dedicated database/collection for logs to avoid contention with application data.

Full Example

python
from logly import logger
from logly.integrations.mongodb import MongoHandler

handler = MongoHandler(
    uri="mongodb://user:pass@mongo-host:27017/",
    database="production",
    collection="app_logs",
    timeout=10.0,
)
logger.add(handler, level="INFO")

logger.info("User signed up", user_id="u-456")
logger.exception("Payment failed")

Released under the MIT License.