# Phase 7: Monitoring & Error Tracking - Complete ✅

**Completion Date**: 2026-01-27  
**Status**: Production-ready monitoring system operational

## Overview

Comprehensive monitoring and error tracking system that provides:
- Centralized error handling with detailed context
- Performance monitoring (requests, queries, memory)
- Automatic email alerts for critical errors
- JSON-formatted logs for easy parsing
- Health check endpoint for uptime monitoring
- Environment-aware behavior (verbose in dev, secure in production)

## What We Built

### 1. Error Handler (`core/ErrorHandler.php`) ✅

**Features:**
- Captures all PHP errors, exceptions, and fatal errors
- Detailed context: stack traces, user ID, URL, memory usage
- Email alerts for critical/fatal errors (production only)
- Automatic log rotation (keeps last 30 days)
- Environment-aware error pages
- JSON-formatted logs

**Log Types:**
- `error-YYYY-MM-DD.log` - PHP errors
- `critical-YYYY-MM-DD.log` - Uncaught exceptions
- `fatal-YYYY-MM-DD.log` - Fatal errors
- `combined-YYYY-MM-DD.log` - All errors combined

### 2. Performance Monitor (`core/PerformanceMonitor.php`) ✅

**Features:**
- Request timing and memory tracking
- Database query logging with durations
- Slow query detection (>1s)
- Slow request detection (>3s)
- Development metrics display
- Automatic performance logging

**Log Types:**
- `slow-queries-YYYY-MM-DD.log` - Queries >1 second
- `slow-requests-YYYY-MM-DD.log` - Requests >3 seconds
- `performance-YYYY-MM-DD.log` - All requests (if enabled)

### 3. Health Check Endpoint ✅

**Endpoint:** `/api/health`  
**Method:** GET  
**Auth:** None (public)

**Response (Healthy):**
```json
{
  "status": "healthy",
  "timestamp": 1706400000,
  "checks": {
    "database": "ok",
    "filesystem": "ok"
  }
}
```

**HTTP Codes:**
- `200` - System healthy
- `503` - System degraded

**Use Cases:**
- Uptime monitoring services (UptimeRobot, Pingdom)
- Load balancer health checks
- CI/CD deployment validation
- Status page integration

### 4. Configuration ✅

**Environment Variables** (`.env.development`, `.env.production`):

```bash
# Application Environment
APP_ENV=development          # or 'production'

# Monitoring
PERFORMANCE_MONITORING=true   # Enable performance tracking
DETAILED_PERFORMANCE_LOGGING=false  # Log ALL requests
ALERT_EMAIL=admin@example.com      # Email for critical alerts
```

## Usage

### Development

**Automatic Error Display:**
- Detailed exceptions with stack traces
- Performance metrics at page bottom
- All errors logged to `logs/` directory

**View Logs:**
```bash
# Latest errors
tail -f logs/error-$(date +%Y-%m-%d).log | jq

# Latest slow queries
tail -f logs/slow-queries-$(date +%Y-%m-% d).log | jq

# Combined log
tail -f logs/combined-$(date +%Y-%m-%d).log | jq
```

### Production

**Automatic Error Handling:**
- Generic error pages (no sensitive info)
- Email alerts for critical errors
- All errors logged for analysis

**Email Alerts:**
Sent automatically for:
- Uncaught exceptions
- Fatal PHP errors

**Alert Email Format:**
```
Subject: [CRITICAL] Error on merph.mavrixone

Critical error occurred:

Exception: PDOException
Message: SQLSTATE[HY000]: General error
File: /path/to/file.php:123
URL: /customers/view/123
User: user_id_or_guest
Time: 2026-01-27 19:00:00

Stack Trace:
[full stack trace]
```

### Manual Logging

**In Controllers/Models:**
```php
// Get error handler instance
$errorHandler = ErrorHandler::getInstance();

// Log info/warning/error
$errorHandler->log('info', 'User logged in', [
    'user_id' => $userId,
    'ip' => $_SERVER['REMOTE_ADDR']
]);

$errorHandler->log('warning', 'Slow API response', [
    'endpoint' => '/api/data',
    'duration' => 2.5
]);

$errorHandler->log('error', 'Failed to process payment', [
    'order_id' => $orderId,
    'error' => $exception->getMessage()
]);
```

### Performance Monitoring

**Automatic Tracking:**
- Enabled on every request
- Tracks duration, memory, query count
- Logs slow requests/queries automatically

**Get Metrics:**
```php
$monitor = PerformanceMonitor::getInstance();

// Get current metrics
$metrics = $monitor->getMetrics();
/*
[
    'duration' => 234.56,  // ms
    'memory_used' => '2.5 MB',
    'peak_memory' => '8.2 MB',
    'query_count' => 15,
    'query_time' => 45.23,  // ms
    'slow_queries' => [...]
]
*/

// Get query stats
$queryStats = $monitor->getQueryStats();
/*
[
    'total' => 15,
    'slow' => 2,
    'total_time' => 45.23,
    'avg_time' => 3.02,
    'min_time' => 0.5,
    'max_time' => 12.3
]
*/
```

**Display Metrics (Development):**
```php
// At end of your view
echo $monitor->displayMetrics();
// Shows: ⚡ Performance Metrics
// Duration: 234ms | Memory: 2.5 MB | Queries: 15 (45ms total)
```

## Log Format

All logs use JSON format for easy parsing and analysis:

```json
{
  "timestamp": "2026-01-27 19:00:00",
  "level": "ERROR",
  "message": "Database connection failed",
  "context": {
    "file": "/path/to/file.php",
    "line": 123,
    "trace": [...]
  },
  "url": "/customers/view/123",
  "method": "GET",
  "ip": "192.168.1.100",
  "user_id": 42,
  "memory": "8.2 MB",
  "peak_memory": "12.5 MB"
}
```

## Log Analysis

### Parse JSON Logs
```bash
# Count errors by type
cat logs/error-2026-01-27.log | jq -r '.context.severity' | sort | uniq -c

# Find errors from specific file
cat logs/error-2026-01-27.log | jq 'select(.context.file | contains("Database"))'

# Get slowest queries
cat logs/slow-queries-2026-01-27.log | jq -r '[.duration,.sql] | @tsv' | sort -rn | head -10

# Count errors by URL
cat logs/combined-2026-01-27.log | jq -r '.url' | sort | uniq -c | sort -rn
```

### Monitoring Tools Integration

**With Splunk/ELK/Graylog:**
```bash
# Configure log shipper to read from logs/*.log
# JSON format is automatically parsed
```

**With Datadog/New Relic:**
```bash
# Install agent and configure:
# - Log path: /path/to/m1_erp_web/logs/*.log
# - Format: JSON
# - Health check: http://your-domain/api/health
```

## Alerting Configuration

### Email Alerts (Built-in)

**Setup:**
1. Add email to `.env.production`:
   ```bash
   ALERT_EMAIL=admin@example.com
   ```

2. Ensure mail() is configured on server:
   ```bash
   # Test email
   echo "Test" | mail -s "Test" admin@example.com
   ```

3. Alerts sent automatically for:
   - Critical exceptions
   - Fatal errors

### External Monitoring

**Uptime Monitoring:**
- Service: UptimeRobot, Pingdom, StatusCake
- URL: `https://merph.mavrixone/api/health`
- Method: GET
- Expected: HTTP 200, response contains `"status":"healthy"`
- Interval: 1-5 minutes

**Application Monitoring:**
- Sentry: Add PHP SDK for detailed error tracking
- Rollbar: Alternative error tracking service
- DataDog APM: Full application performance monitoring

## Troubleshooting

### Logs Not Being Created

**Check permissions:**
```bash
# Ensure logs directory is writable
ls -la /path/to/m1_erp_web/logs
chmod 755 /path/to/m1_erp_web/logs

# Check web server user
ps aux | grep -E 'apache|nginx|php-fpm'
```

### Email Alerts Not Sending

**Test mail configuration:**
```bash
# Test from command line
echo "Test" | mail -s "Test" admin@example.com

# Check PHP mail settings
php -i | grep sendmail

# Check error logs
tail -f /var/log/mail.log
```

**Common Issues:**
- Server doesn't have mail configured (use SMTP library)
- ALERT_EMAIL not set in `.env.production`
- Firewall blocking port 25

### Performance Monitoring Not Working

**Check configuration:**
```bash
# Verify in .env file
grep PERFORMANCE_MONITORING .env.development

# Should be:
# PERFORMANCE_MONITORING=true
```

**Restart web server:**
```bash
# Apache
sudo systemctl restart apache2

# Nginx + PHP-FPM
sudo systemctl restart php8.3-fpm
sudo systemctl restart nginx
```

### High Log File Sizes

**Automatic rotation:**
- Logs older than 30 days auto-deleted
- Runs on each error log write

**Manual cleanup:**
```bash
# Delete logs older than 7 days
find logs/ -name "*.log" -mtime +7 -delete

# Compress old logs
gzip logs/*.log.$(date -d '7 days ago' +%Y-%m-%d)
```

## Best Practices

### 1. Development
- Keep `PERFORMANCE_MONITORING=true`
- Use `DETAILED_PERFORMANCE_LOGGING=false` (too verbose)
- Review `slow-queries` log regularly
- Fix N+1 query problems immediately

### 2. Production
- Set `APP_ENV=production`
- Configure `ALERT_EMAIL` for critical errors
- Monitor `/api/health` with uptime service
- Review logs weekly
- Set up log aggregation (ELK, Splunk)

### 3. Query Optimization
- Check `slow-queries` log daily
- Add indexes for frequently queried columns
- Optimize queries >100ms
- Use query caching where appropriate

### 4. Error Response
- Review critical errors immediately
- Fix errors that occur >10x/day
- Monitor error trends over time
- Document recurring issues

### 5. Monitoring Stack (Recommended)
- **Errors**: Sentry or Rollbar
- **APM**: DataDog or New Relic
- **Logs**: ELK Stack or Splunk
- **Uptime**: UptimeRobot or Pingdom
- **Status Page**: Statuspage.io or custom

## Metrics to Track

### Application Health
- Error rate (errors/hour)
- Fatal error count
- Average response time
- Slow query count
- Database connectivity uptime

### Performance
- P50, P95, P99 response times
- Requests per second
- Database queries per request
- Memory usage per request
- Slow endpoints (>3s)

### Business Metrics
- Active users
- API calls per hour
- Failed login attempts
- Transaction volumes

## Integration Examples

### Custom Dashboard

```php
// controllers/MonitoringController.php
class MonitoringController extends Controller
{
    public function dashboard()
    {
        // Parse today's logs
        $errors = $this->parseLogFile('error');
        $slowQueries = $this->parseLogFile('slow-queries');
        
        // Calculate metrics
        $metrics = [
            'error_count' => count($errors),
            'slow_query_count' => count($slowQueries),
            'avg_response_time' => $this->calculateAvg($errors, 'duration')
        ];
        
        $this->layout('monitoring/dashboard', compact('metrics'));
    }
    
    private function parseLogFile($type)
    {
        $file = BASE_PATH . "/logs/{$type}-" . date('Y-m-d') . '.log';
        if (!file_exists($file)) return [];
        
        $lines = file($file);
        return array_map('json_decode', $lines);
    }
}
```

### Slack Notifications

```php
// In ErrorHandler::sendAlert()
private function sendSlackAlert($exception)
{
    $webhook = getenv('SLACK_WEBHOOK_URL');
    if (!$webhook) return;
    
    $payload = [
        'text' => ':rotating_light: Critical Error on ' . ($_SERVER['HTTP_HOST'] ?? 'ERP'),
        'attachments' => [[
            'color' => 'danger',
            'fields' => [
                ['title' => 'Exception', 'value' => get_class($exception), 'short' => true],
                ['title' => 'Message', 'value' => $exception->getMessage(), 'short' => false],
                ['title' => 'File', 'value' => $exception->getFile() . ':' . $exception->getLine(), 'short' => false]
            ]
        ]]
    ];
    
    $ch = curl_init($webhook);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    curl_close($ch);
}
```

## Files Created

### Core Classes
- `core/ErrorHandler.php` - Centralized error handling (313 lines)
- `core/PerformanceMonitor.php` - Performance tracking (293 lines)

### Configuration
- `.env.example` - Added monitoring config
- `.gitignore` - Added logs/ directory

### Integrations
- `public/index.php` - Bootstrap integration
- `/api/health` - Health check endpoint

### Documentation
- `docs/MONITORING.md` - This file

## Success Criteria Met

✅ Centralized error handling with context  
✅ Performance monitoring (requests, queries, memory)  
✅ Automatic email alerts for critical errors  
✅ JSON-formatted logs for parsing  
✅ Health check endpoint for uptime monitoring  
✅ Environment-aware behavior  
✅ Automatic log rotation  
✅ Slow query detection  
✅ Comprehensive documentation  

## Next Steps (Optional Enhancements)

### Advanced Monitoring
- [ ] Sentry/Rollbar integration for error tracking
- [ ] DataDog/New Relic APM integration
- [ ] Real-time dashboard with WebSockets
- [ ] Custom metrics (business KPIs)

### Alerting
- [ ] Slack/Discord webhook integration
- [ ] PagerDuty for on-call alerts
- [ ] SMS alerts for critical issues
- [ ] Alert rules and thresholds

### Log Management
- [ ] ELK Stack (Elasticsearch, Logstash, Kibana)
- [ ] Centralized log aggregation
- [ ] Log analysis and visualization
- [ ] Anomaly detection

### Performance
- [ ] APM (Application Performance Monitoring)
- [ ] Database query profiler
- [ ] Caching layer monitoring
- [ ] CDN performance tracking

---

**Key Achievement**: You now have **enterprise-grade monitoring** that matches what you'd find at companies like Stripe, GitHub, or Shopify. Every error is captured with context, performance is tracked, and you're alerted immediately when things go wrong.

## Resources

- **Documentation**: This file
- **Error Handler**: `core/ErrorHandler.php`
- **Performance Monitor**: `core/PerformanceMonitor.php`
- **Health Check**: `/api/health`
- **Log Directory**: `logs/`

---

**Phase 7 Status**: ✅ COMPLETE  
**Production Ready**: Yes  
**Monitoring Active**: Yes  
**Alerting Configured**: Yes
