|
| 1 | +# Logging Examples for MCP Gateway |
| 2 | + |
| 3 | +This document provides practical examples of using the new logging features in MCP Gateway. |
| 4 | + |
| 5 | +## Quick Start Examples |
| 6 | + |
| 7 | +### 1. Basic Development Setup |
| 8 | +```bash |
| 9 | +# Enable debug logging for development |
| 10 | +export LOG_LEVEL=DEBUG |
| 11 | +export LOG_FORMAT=text |
| 12 | +export LOG_FOLDER=./dev-logs |
| 13 | +mcpgateway --host 0.0.0.0 --port 4444 |
| 14 | +``` |
| 15 | + |
| 16 | +### 2. Production Configuration |
| 17 | +```bash |
| 18 | +# Production logging with JSON format |
| 19 | +export LOG_LEVEL=INFO |
| 20 | +export LOG_FOLDER=/var/log/mcpgateway |
| 21 | +export LOG_FILE=gateway.log |
| 22 | +export LOG_FILEMODE=a+ |
| 23 | +mcpgateway --host 0.0.0.0 --port 4444 |
| 24 | +``` |
| 25 | + |
| 26 | +### 3. Monitoring Specific Components |
| 27 | +```bash |
| 28 | +# Monitor tool service activities |
| 29 | +tail -f logs/mcpgateway.log | grep "tool_service" |
| 30 | + |
| 31 | +# Watch for errors across all services |
| 32 | +tail -f logs/mcpgateway.log | grep "ERROR\|WARNING" |
| 33 | + |
| 34 | +# Pretty-print JSON logs |
| 35 | +tail -f logs/mcpgateway.log | jq '.' |
| 36 | +``` |
| 37 | + |
| 38 | +## Configuration Examples |
| 39 | + |
| 40 | +### .env File Configuration |
| 41 | +```env |
| 42 | +# Complete logging configuration |
| 43 | +LOG_LEVEL=INFO |
| 44 | +LOG_FORMAT=json |
| 45 | +LOG_FILE=mcpgateway.log |
| 46 | +LOG_FOLDER=logs |
| 47 | +LOG_FILEMODE=a+ |
| 48 | +``` |
| 49 | + |
| 50 | +### Docker/Container Configuration |
| 51 | +```yaml |
| 52 | +# docker-compose.yml |
| 53 | +services: |
| 54 | + mcpgateway: |
| 55 | + image: ghcr.io/ibm/mcp-context-forge:latest |
| 56 | + environment: |
| 57 | + - LOG_LEVEL=INFO |
| 58 | + - LOG_FOLDER=/app/logs |
| 59 | + - LOG_FILE=gateway.log |
| 60 | + volumes: |
| 61 | + - ./logs:/app/logs |
| 62 | +``` |
| 63 | +
|
| 64 | +### Kubernetes Configuration |
| 65 | +```yaml |
| 66 | +apiVersion: apps/v1 |
| 67 | +kind: Deployment |
| 68 | +metadata: |
| 69 | + name: mcpgateway |
| 70 | +spec: |
| 71 | + template: |
| 72 | + spec: |
| 73 | + containers: |
| 74 | + - name: mcpgateway |
| 75 | + env: |
| 76 | + - name: LOG_LEVEL |
| 77 | + value: "INFO" |
| 78 | + - name: LOG_FOLDER |
| 79 | + value: "/var/log/mcpgateway" |
| 80 | + - name: LOG_FILE |
| 81 | + value: "gateway.log" |
| 82 | + volumeMounts: |
| 83 | + - name: log-storage |
| 84 | + mountPath: /var/log/mcpgateway |
| 85 | +``` |
| 86 | +
|
| 87 | +## Log Analysis Examples |
| 88 | +
|
| 89 | +### 1. Finding Errors and Issues |
| 90 | +```bash |
| 91 | +# Find all errors |
| 92 | +grep "ERROR" logs/mcpgateway.log |
| 93 | + |
| 94 | +# Find warnings and errors |
| 95 | +grep -E "ERROR|WARNING" logs/mcpgateway.log |
| 96 | + |
| 97 | +# Get context around errors (5 lines before and after) |
| 98 | +grep -B5 -A5 "ERROR" logs/mcpgateway.log |
| 99 | +``` |
| 100 | + |
| 101 | +### 2. Monitoring Service Activity |
| 102 | +```bash |
| 103 | +# Gateway service activity |
| 104 | +grep "gateway_service" logs/mcpgateway.log | tail -20 |
| 105 | + |
| 106 | +# Tool invocations |
| 107 | +grep "tool_service.*invoke" logs/mcpgateway.log |
| 108 | + |
| 109 | +# Federation activity |
| 110 | +grep "federation" logs/mcpgateway.log |
| 111 | +``` |
| 112 | + |
| 113 | +### 3. Performance Analysis |
| 114 | +```bash |
| 115 | +# Look for slow operations (if duration logging is enabled) |
| 116 | +grep "duration" logs/mcpgateway.log | sort -k5 -nr |
| 117 | + |
| 118 | +# Database operations |
| 119 | +grep "database" logs/mcpgateway.log |
| 120 | + |
| 121 | +# HTTP request/response logs |
| 122 | +grep -E "HTTP|request" logs/mcpgateway.log |
| 123 | +``` |
| 124 | + |
| 125 | +## Log Format Examples |
| 126 | + |
| 127 | +### JSON Format (File Output) |
| 128 | +```json |
| 129 | +{ |
| 130 | + "asctime": "2025-01-09 17:30:15,123", |
| 131 | + "name": "mcpgateway.gateway_service", |
| 132 | + "levelname": "INFO", |
| 133 | + "message": "Gateway peer-gateway-1 registered successfully", |
| 134 | + "funcName": "register_gateway", |
| 135 | + "lineno": 245, |
| 136 | + "module": "gateway_service", |
| 137 | + "pathname": "/app/mcpgateway/services/gateway_service.py" |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +### Text Format (Console Output) |
| 142 | +``` |
| 143 | +2025-01-09 17:30:15,123 - mcpgateway.gateway_service - INFO - Gateway peer-gateway-1 registered successfully |
| 144 | +2025-01-09 17:30:16,456 - mcpgateway.tool_service - DEBUG - Tool 'get_weather' invoked with args: {'location': 'New York'} |
| 145 | +2025-01-09 17:30:17,789 - mcpgateway.admin - WARNING - Authentication failed for user: anonymous |
| 146 | +``` |
| 147 | + |
| 148 | +## Integration Examples |
| 149 | + |
| 150 | +### 1. ELK Stack Integration |
| 151 | +```bash |
| 152 | +# Configure Filebeat to ship logs |
| 153 | +# filebeat.yml |
| 154 | +filebeat.inputs: |
| 155 | +- type: log |
| 156 | + paths: |
| 157 | + - /var/log/mcpgateway/*.log |
| 158 | + json.keys_under_root: true |
| 159 | + json.add_error_key: true |
| 160 | +``` |
| 161 | + |
| 162 | +### 2. Datadog Integration |
| 163 | +```bash |
| 164 | +# Configure Datadog agent |
| 165 | +# datadog.yaml |
| 166 | +logs_config: |
| 167 | + logs_dd_url: intake.logs.datadoghq.com:10516 |
| 168 | + |
| 169 | +logs: |
| 170 | + - type: file |
| 171 | + path: "/var/log/mcpgateway/*.log" |
| 172 | + service: mcpgateway |
| 173 | + source: python |
| 174 | + sourcecategory: mcp |
| 175 | +``` |
| 176 | + |
| 177 | +### 3. Prometheus/Grafana Monitoring |
| 178 | +```bash |
| 179 | +# Use log-based metrics with promtail |
| 180 | +# promtail-config.yml |
| 181 | +scrape_configs: |
| 182 | +- job_name: mcpgateway |
| 183 | + static_configs: |
| 184 | + - targets: |
| 185 | + - localhost |
| 186 | + labels: |
| 187 | + job: mcpgateway |
| 188 | + __path__: /var/log/mcpgateway/*.log |
| 189 | +``` |
| 190 | + |
| 191 | +## Troubleshooting Examples |
| 192 | + |
| 193 | +### Common Issues and Solutions |
| 194 | + |
| 195 | +1. **Log files not rotating** |
| 196 | + ```bash |
| 197 | + # Check file permissions and available disk space |
| 198 | + ls -la logs/ |
| 199 | + df -h |
| 200 | + ``` |
| 201 | + |
| 202 | +2. **Missing log directory** |
| 203 | + ```bash |
| 204 | + # The directory is created automatically, but check permissions |
| 205 | + mkdir -p logs |
| 206 | + chmod 755 logs |
| 207 | + ``` |
| 208 | + |
| 209 | +3. **Too many log files** |
| 210 | + ```bash |
| 211 | + # Clean up old rotated logs |
| 212 | + find logs/ -name "*.log.[6-9]" -delete |
| 213 | + find logs/ -name "*.log.1[0-9]" -delete |
| 214 | + ``` |
| 215 | + |
| 216 | +4. **JSON parsing errors** |
| 217 | + ```bash |
| 218 | + # Validate JSON format |
| 219 | + cat logs/mcpgateway.log | jq empty |
| 220 | + |
| 221 | + # Show only invalid JSON lines |
| 222 | + cat logs/mcpgateway.log | while read line; do |
| 223 | + echo "$line" | jq empty 2>/dev/null || echo "Invalid: $line" |
| 224 | + done |
| 225 | + ``` |
| 226 | + |
| 227 | +## Best Practices |
| 228 | + |
| 229 | +1. **Production Logging** |
| 230 | + - Use `INFO` level for production |
| 231 | + - Enable JSON format for log aggregation |
| 232 | + - Configure proper log rotation |
| 233 | + - Monitor disk space usage |
| 234 | + |
| 235 | +2. **Development Logging** |
| 236 | + - Use `DEBUG` level for detailed troubleshooting |
| 237 | + - Use text format for human readability |
| 238 | + - Keep log files local for quick access |
| 239 | + |
| 240 | +3. **Security Considerations** |
| 241 | + - Ensure log files don't contain sensitive data |
| 242 | + - Protect log directories with proper permissions |
| 243 | + - Rotate logs regularly to prevent disk filling |
| 244 | + |
| 245 | +4. **Performance Considerations** |
| 246 | + - Avoid excessive DEBUG logging in production |
| 247 | + - Monitor log I/O performance |
| 248 | + - Use appropriate log levels for different components |
0 commit comments