|
| 1 | +# SQL Timeout Logging |
| 2 | + |
| 3 | +This document explains how to log SQL requests that are timing out or approaching timeout limits in Lychee. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +When running with Laravel Octane (FrankenPHP/Swoole/RoadRunner), requests have a maximum execution time (default: 30 seconds as configured in `config/octane.php`). SQL queries that take too long can cause the entire request to timeout. |
| 8 | + |
| 9 | +The logging system tracks: |
| 10 | +1. **Slow queries** - Queries taking longer than configured threshold |
| 11 | +2. **Queries approaching timeout** - Queries using >70% of max execution time |
| 12 | +3. **Critical queries** - Queries using >90% of max execution time |
| 13 | +4. **PHP timeouts** - When the entire request times out |
| 14 | + |
| 15 | +## Configuration |
| 16 | + |
| 17 | +### Enable SQL Logging |
| 18 | + |
| 19 | +Set the following in your `.env` file: |
| 20 | + |
| 21 | +```env |
| 22 | +# Enable SQL query logging |
| 23 | +DB_LOG_SQL=true |
| 24 | +
|
| 25 | +# Optional: Minimum execution time to log (in milliseconds, default: 100) |
| 26 | +DB_LOG_SQL_MIN_TIME=100 |
| 27 | +
|
| 28 | +# Optional: Enable EXPLAIN for MySQL SELECT queries |
| 29 | +DB_LOG_SQL_EXPLAIN=true |
| 30 | +``` |
| 31 | + |
| 32 | +### Timeout Settings |
| 33 | + |
| 34 | +The max execution time is configured in `config/octane.php`: |
| 35 | + |
| 36 | +```php |
| 37 | +'max_execution_time' => 30, // seconds |
| 38 | +``` |
| 39 | + |
| 40 | +## How It Works |
| 41 | + |
| 42 | +### 1. Query Execution Logging |
| 43 | + |
| 44 | +**Location**: [app/Providers/AppServiceProvider.php:238-300](app/Providers/AppServiceProvider.php#L238-L300) |
| 45 | + |
| 46 | +The `logSQL()` method logs queries after they complete with severity based on execution time: |
| 47 | +- **Debug**: Normal slow queries (>100ms) |
| 48 | +- **Warning**: Queries taking >1 second |
| 49 | +- **Error**: Queries approaching timeout (>90% of max_execution_time) |
| 50 | + |
| 51 | +### 2. Timeout Detection Listener |
| 52 | + |
| 53 | +**Location**: [app/Listeners/LogQueryTimeout.php](app/Listeners/LogQueryTimeout.php) |
| 54 | + |
| 55 | +Registered in [app/Providers/EventServiceProvider.php:97-99](app/Providers/EventServiceProvider.php#L97-L99) |
| 56 | + |
| 57 | +This listener provides detailed logging for queries that exceed warning/critical thresholds: |
| 58 | +- **70% threshold**: WARNING level log |
| 59 | +- **90% threshold**: CRITICAL/ERROR level log |
| 60 | + |
| 61 | +### 3. PHP Timeout Handler |
| 62 | + |
| 63 | +**Location**: [app/Providers/AppServiceProvider.php:200-216](app/Providers/AppServiceProvider.php#L200-L216) |
| 64 | + |
| 65 | +A shutdown function that catches when PHP times out entirely, logging: |
| 66 | +- Error message |
| 67 | +- File and line where timeout occurred |
| 68 | +- Request URL and method |
| 69 | + |
| 70 | +## Log Files |
| 71 | + |
| 72 | +Logs are written to different files based on severity: |
| 73 | + |
| 74 | +- `storage/logs/errors.log` - Critical/slow queries (>90% timeout) |
| 75 | +- `storage/logs/warning.log` - Slow queries (>80% timeout or >1s) |
| 76 | +- `storage/logs/daily.log` - All SQL queries (when DB_LOG_SQL=true) |
| 77 | + |
| 78 | +## Example Log Entries |
| 79 | + |
| 80 | +### Slow Query Warning |
| 81 | +``` |
| 82 | +[2026-01-04 10:15:32] warning.WARNING: ⚠️ WARNING: Slow query detected {"execution_time_ms":21000,"execution_time_s":21,"timeout_limit_s":30,"percentage":"70%","sql":"SELECT * FROM photos WHERE album_id = ?","bindings":[123],"connection":"mysql","url":"https://lychee.local/api/albums/123/photos"} |
| 83 | +``` |
| 84 | + |
| 85 | +### Critical Query Near Timeout |
| 86 | +``` |
| 87 | +[2026-01-04 10:20:45] error.ERROR: 🚨 CRITICAL: Query approaching timeout {"execution_time_ms":27500,"execution_time_s":27.5,"timeout_limit_s":30,"percentage":"91.7%","sql":"SELECT * FROM photos WHERE...","bindings":[...],"connection":"mysql","url":"https://lychee.local/api/..."} |
| 88 | +``` |
| 89 | + |
| 90 | +### PHP Timeout Detected |
| 91 | +``` |
| 92 | +[2026-01-04 10:25:12] error.ERROR: 🔥 PHP TIMEOUT DETECTED {"error":"Maximum execution time of 30 seconds exceeded","file":"/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php","line":742,"url":"https://lychee.local/api/albums/delete","method":"DELETE"} |
| 93 | +``` |
| 94 | + |
| 95 | +## Troubleshooting Timeouts |
| 96 | + |
| 97 | +When you see timeout logs: |
| 98 | + |
| 99 | +1. **Check the SQL query** - Look for missing indexes, inefficient joins, or full table scans |
| 100 | +2. **Use EXPLAIN** - Enable `DB_LOG_SQL_EXPLAIN=true` to see query execution plans |
| 101 | +3. **Add indexes** - Common fixes involve adding database indexes |
| 102 | +4. **Optimize queries** - Rewrite queries to be more efficient |
| 103 | +5. **Increase timeout** - As a last resort, increase `max_execution_time` in `config/octane.php` |
| 104 | +6. **Use queues** - Move long-running operations to background jobs |
| 105 | + |
| 106 | +## Performance Impact |
| 107 | + |
| 108 | +SQL logging has minimal performance impact when disabled. When enabled: |
| 109 | +- Each query execution triggers event listeners |
| 110 | +- EXPLAIN queries add overhead for SELECT statements (MySQL only) |
| 111 | +- Logs are written asynchronously via Monolog |
| 112 | + |
| 113 | +**Recommendation**: Only enable in development or temporarily in production for debugging. |
| 114 | + |
| 115 | +## Related Configuration |
| 116 | + |
| 117 | +### Database Connection Timeouts |
| 118 | + |
| 119 | +MySQL connection settings in [config/database.php:111-113](config/database.php#L111-L113): |
| 120 | + |
| 121 | +```php |
| 122 | +PDO::ATTR_TIMEOUT => 5, // Connection timeout (5 seconds) |
| 123 | +PDO::MYSQL_ATTR_INIT_COMMAND => 'SET SESSION wait_timeout=28800', // 8 hours |
| 124 | +``` |
| 125 | + |
| 126 | +### Octane Database Ping |
| 127 | + |
| 128 | +The AppServiceProvider pings database connections every 30 seconds to prevent timeouts: [app/Providers/AppServiceProvider.php:340-341](app/Providers/AppServiceProvider.php#L340-L341) |
| 129 | + |
| 130 | +## Viewing Logs |
| 131 | + |
| 132 | +Logs can be viewed via: |
| 133 | +1. **Log Viewer** - Built-in at `/log-viewer` (requires admin access) |
| 134 | +2. **Command line**: `tail -f storage/logs/errors.log` |
| 135 | +3. **Docker**: `docker logs <container_name>` |
| 136 | + |
| 137 | +## Additional Notes |
| 138 | + |
| 139 | +- Timeout detection works best with FrankenPHP, Swoole, or RoadRunner |
| 140 | +- Traditional PHP-FPM may not trigger all timeout handlers consistently |
0 commit comments