rishithayanidhi/datacenter-cooling-optimization
0
1# Container Logging System2 3## Overview4 5The Data Center Cooling Optimization Environment now includes a comprehensive logging system to handle container logs, request tracking, and debugging information.6 7## New Endpoints8 9### 1. `GET /logs`10 11Retrieve container logs with optional filtering.12 13**Query Parameters:**14 15- `limit` (int, default: 100): Maximum number of log entries to return16- `level` (str, optional): Filter by log level - `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`17- `format` (str, default: json): Response format - `json` or `text`18 19**Example Requests:**20 21```bash22# Get last 100 logs23curl http://localhost:8000/logs24 25# Get last 50 INFO logs26curl "http://localhost:8000/logs?limit=50&level=INFO"27 28# Get last 20 ERROR logs in text format29curl "http://localhost:8000/logs?limit=20&level=ERROR&format=text"30```31 32**Response (JSON):**33 34```json35{36 "logs": [37 {38 "timestamp": "2026-04-08T08:56:31.123456",39 "level": "INFO",40 "message": "HTTP GET /health",41 "extra": {42 "http_method": "GET",43 "http_path": "/health",44 "http_status": 200,45 "client_ip": "10.16.8.212",46 "type": "http_request"47 }48 }49 ],50 "count": 1,51 "stats": {52 "total_entries": 150,53 "max_size": 10000,54 "start_time": "2026-04-08T08:56:30",55 "uptime_seconds": 1.5,56 "by_level": {57 "INFO": 120,58 "DEBUG": 20,59 "WARNING": 5,60 "ERROR": 561 }62 },63 "filter": {64 "limit": 100,65 "level": null,66 "recent_only": true67 }68}69```70 71---72 73### 2. `GET /logs/container`74 75Get container-specific logs with metadata and health information.76 77**Purpose:** This is the endpoint that external monitoring services (like Hugging Face Spaces) call to fetch container logs.78 79**Example Request:**80 81```bash82curl http://localhost:8000/logs/container83```84 85**Response:**86 87```json88{89 "type": "container",90 "logs": [...],91 "summary": {92 "total": 150,93 "container_info": {94 "hostname": "data-center-cooling-env",95 "uptime": 125.5,96 "log_buffer_size": 10000,97 "log_entries": 150,98 "start_time": "2026-04-08T08:56:30.123456"99 },100 "health": {101 "status": "healthy",102 "error_count": 2,103 "critical_count": 0,104 "total_logs": 150,105 "timestamp": "2026-04-08T08:58:15.654321"106 }107 }108}109```110 111---112 113### 3. `GET /logs/stats`114 115Get detailed logging statistics and container health status.116 117**Example Request:**118 119```bash120curl http://localhost:8000/logs/stats121```122 123**Response:**124 125```json126{127 "container_info": {128 "hostname": "data-center-cooling-env",129 "uptime": 125.5,130 "log_buffer_size": 10000,131 "log_entries": 150,132 "start_time": "2026-04-08T08:56:30.123456"133 },134 "health": {135 "status": "healthy",136 "error_count": 2,137 "critical_count": 0,138 "total_logs": 150,139 "timestamp": "2026-04-08T08:58:15.654321"140 },141 "stats": {142 "total_entries": 150,143 "max_size": 10000,144 "start_time": "2026-04-08T08:56:30",145 "uptime_seconds": 125.5,146 "by_level": {147 "INFO": 120,148 "DEBUG": 20,149 "WARNING": 5,150 "ERROR": 5151 }152 }153}154```155 156**Health Status Values:**157 158- `healthy`: No errors or critical issues (✓ normal operation)159- `degraded`: More than 10 errors detected (⚠ warning)160- `critical`: One or more critical errors (✗ needs attention)161 162---163 164### 4. `GET /logs/clear`165 166Clear all stored logs (development only).167 168**Example Request:**169 170```bash171curl http://localhost:8000/logs/clear172```173 174**Response:**175 176```json177{178 "status": "success",179 "message": "All logs have been cleared",180 "timestamp": "2026-04-08T08:58:15.654321"181}182```183 184---185 186## Logging Middleware187 188All HTTP requests are automatically logged by the `LoggingMiddleware`. Each request creates an entry with:189 190- HTTP method (GET, POST, etc.)191- Request path192- Status code (200, 404, 500, etc.)193- Client IP address194- Timestamp195 196### Example Log Entry:197 198```json199{200 "timestamp": "2026-04-08T08:56:31.123456",201 "level": "INFO",202 "message": "HTTP GET /health",203 "extra": {204 "http_method": "GET",205 "http_path": "/health",206 "http_status": 200,207 "client_ip": "10.16.8.212",208 "type": "http_request"209 }210}211```212 213---214 215## Log Buffer216 217The logging system maintains an in-memory circular buffer of the most recent 10,000 log entries. This provides:218 2191. **Fast Access:** No need to read from disk2202. **Memory Efficient:** Fixed size buffer prevents unbounded growth2213. **Recent Data:** Always keeps the most relevant recent logs2224. **Graceful Overflow:** Oldest entries are automatically discarded when buffer is full223 224### Configuration225 226To change the buffer size, modify `logging_service.py`:227 228```python229_container_logger = ContainerLogger(max_buffer_size=20000) # Increase to 20k entries230```231 232---233 234## Integration with External Monitoring235 236Services like Hugging Face Spaces can monitor container health by calling `/logs/container` periodically. The response includes:237 2381. Recent logs (up to 200 entries)2392. Container metadata (uptime, hostname)2403. Health status (healthy/degraded/critical)2414. Error and critical event counts242 243### Example Monitoring Script:244 245```python246import requests247import time248 249def monitor_container():250 while True:251 try:252 response = requests.get("http://localhost:8000/logs/container")253 data = response.json()254 255 health = data["summary"]["health"]256 print(f"Container Health: {health['status']}")257 print(f"Errors: {health['error_count']}, Critical: {health['critical_count']}")258 259 if health["status"] == "critical":260 # Alert or take action261 print("⚠️ CRITICAL HEALTH STATUS DETECTED!")262 263 except Exception as e:264 print(f"Error fetching logs: {e}")265 266 time.sleep(30) # Check every 30 seconds267 268if __name__ == "__main__":269 monitor_container()270```271 272---273 274## Python API275 276### Using the Logger in Your Code277 278```python279from server.logging_service import (280 get_container_logger,281 log_request,282 log_environment_event,283 log_websocket_event,284)285 286# Get the logger instance287logger = get_container_logger()288 289# Log different levels290logger.debug("Debug information", extra={"key": "value"})291logger.info("Information message")292logger.warning("Warning message")293logger.error("Error message")294logger.critical("Critical error")295 296# Helper functions for specific event types297log_request(method="POST", path="/reset", status_code=200, client_ip="127.0.0.1")298log_environment_event("episode_completed", {"reward": 350.5, "steps": 50})299log_websocket_event("connected", client_id="client_123", details={"version": "1.0"})300 301# Get logs programmatically302logs = logger.get_logs(limit=50, level="ERROR")303print(f"Total errors: {logs['count']}")304 305# Get health status306health = logger.get_health_status()307print(f"Container status: {health['status']}")308```309 310---311 312## Environment Variables313 314No special environment variables are required for logging. The system works automatically. However, you can control the logger behavior through code:315 316```python317from server.logging_service import get_container_logger318 319logger = get_container_logger()320 321# Clear logs periodically if needed322logger.buffer.clear()323 324# Get statistics325stats = logger.buffer.get_stats()326print(stats)327```328 329---330 331## Production Recommendations332 3331. **Log Persistence:** For production, consider integrating with logging services (Sentry, CloudWatch, Datadog, etc.)3342. **Log Rotation:** Current system uses in-memory buffer; add file logging for long-term retention3353. **Security:** The `/logs/clear` endpoint should be protected with authentication in production3364. **Performance:** The logging middleware adds minimal overhead (~1-2ms per request)337 338### Example: Adding File Logging339 340```python341import logging.handlers342 343handler = logging.handlers.RotatingFileHandler(344 "app.log",345 maxBytes=10*1024*1024, # 10MB346 backupCount=5347)348logger.logger.addHandler(handler)349```350 351---352 353## Troubleshooting354 355### Issue: 404 errors on `/logs` endpoints356 357**Solution:** Ensure the server is running with the latest app.py that includes the logging endpoints.358 359### Issue: Logs endpoint returns empty360 361**Solution:** This is normal if no requests have been made since startup. Make a request to any endpoint (e.g., `/health`) to generate logs.362 363### Issue: High memory usage364 365**Solution:** The buffer is capped at 10,000 entries by default. If it grows too large, it automatically discards old entries. You can also call `/logs/clear` to reset it.366 367---368 369## Web Interface370 371The web interface at `/web` includes quick links to:372 373- 📋 Container Logs (`/logs?limit=50`)374- 📊 Logging Stats (`/logs/stats`)375 376Visit [http://localhost:8000/web](http://localhost:8000/web) to access these links.377 378---379 380## API Documentation381 382Full interactive API documentation available at:383 384- **Swagger UI**: [http://localhost:8000/docs](http://localhost:8000/docs)385- **ReDoc**: [http://localhost:8000/redoc](http://localhost:8000/redoc)386 387The logging endpoints are documented in the `/logs` section.388 