Spaces:
Build error
Build error
File size: 632 Bytes
64eae68 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import logging
import pymongo
from datetime import datetime
from dotenv import load_dotenv
import os
load_dotenv()
# MongoDB setup
client = pymongo.MongoClient(os.getenv("MONGO_URI"))
db = client["Capstone"]
log_collection = db["logs"]
class MongoHandler(logging.Handler):
def emit(self, record):
log = {
"level": record.levelname,
"message": record.getMessage(),
"timestamp": datetime.now()
}
log_collection.insert_one(log)
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("ProjectLogger")
logger.addHandler(MongoHandler())
|