π MongoDB Logging Deployment Guide for Hugging Face Spaces
β What's Ready for Deployment
π Files Added/Modified:
- β
mongodb_logging.py- Custom MongoDB logging handler - β
server.py- Updated with MongoDB logging integration - β
requirements.txt- Already includespymongo[srv]
π§ New API Endpoints:
GET /logs- View all logs (metadata + application logs)GET /logs/stats- View logging statisticsGET /logs/clear?days_older_than=7- Clear old logs
π Hugging Face Spaces Deployment Steps
1. Set Environment Variables in Hugging Face Spaces
Go to your Space settings and add these environment variables:
MONGO_URI=mongodb+srv://harilogicgo_db_user:SSbZ55jNUsVerWKI@hairswapdb.7r2ghs4.mongodb.net/?retryWrites=true&w=majority&appName=HairSwapDB
2. Push Changes to Hugging Face
# Add all changes
git add .
# Commit changes
git commit -m "feat: add MongoDB logging for all application logs
- Add mongodb_logging.py with custom logging handler
- Update server.py to integrate MongoDB logging
- Add new endpoints: /logs, /logs/stats, /logs/clear
- All application logs now stored in MongoDB HairSwapDB.logs collection"
# Push to Hugging Face
git push origin main
3. Verify Deployment
After deployment, test these endpoints:
- Health Check:
GET /health - View Logs:
GET /logs - Log Statistics:
GET /logs/stats - Clear Old Logs:
GET /logs/clear?days_older_than=7
π MongoDB Collections Structure
HairSwapDB.logs Collection:
{
"_id": "ObjectId",
"timestamp": "2025-09-27T12:30:45.123Z",
"level": "INFO|WARNING|ERROR",
"logger": "hair_server",
"message": "Log message content",
"module": "server",
"function": "get_hairswap",
"line": 168,
"thread": 12345,
"process": 6789,
"exception": "Exception traceback (if error)"
}
HairSwapDB.uploads Collection:
{
"_id": "image-uuid",
"filename": "image.png",
"path": "/data/uploads/image-uuid.png"
}
HairSwapDB.results Collection:
{
"_id": "result-uuid",
"filename": "result.png",
"path": "/data/results/result-uuid.png",
"source_id": "source-uuid",
"reference_id": "reference-uuid"
}
π Testing the Deployment
1. Test Log Generation:
# Upload an image
curl -X POST "https://your-space-url/upload" \
-F "image=@test_image.jpg"
# Perform hair swap
curl -X POST "https://your-space-url/get-hairswap" \
-H "Content-Type: application/json" \
-d '{"source_id": "source-uuid", "reference_id": "reference-uuid"}'
2. Check Logs:
# View all logs
curl "https://your-space-url/logs"
# View log statistics
curl "https://your-space-url/logs/stats"
# Clear logs older than 7 days
curl "https://your-space-url/logs/clear?days_older_than=7"
π― What Gets Logged
β Application Logs (NEW):
- Model loading messages
- Hair transfer progress
- Error messages and stack traces
- API request/response logs
- System status messages
β Metadata Logs (EXISTING):
- Image uploads
- Hair swap results
- File paths and IDs
π¨ Important Notes
- MongoDB Connection: Make sure
MONGO_URIis set in Space environment variables - Log Rotation: Use
/logs/clearendpoint to manage log storage - Performance: MongoDB logging is asynchronous and won't slow down the API
- Fallback: If MongoDB fails, logs still go to console
- Security: MongoDB URI contains credentials - keep it secure
π Ready to Deploy!
Your MongoDB logging system is ready for Hugging Face Spaces deployment. All application logs will be automatically stored in MongoDB and accessible via the new API endpoints.