This implementation automatically captures every error in your Laravel application and stores it in Elasticsearch, providing powerful search capabilities, real-time monitoring, and comprehensive error analytics that will transform how you debug and maintain your applications.
- β¨ Features
- π§ Prerequisites
- π Installation
- βοΈ Configuration
- π» Usage
- π‘οΈ Security Features
- π Monitoring & Analytics
- π API Reference
- β Best Practices
- π§ Troubleshooting
- π€ Contributing
- π Related Articles
- π― Automatic Exception Handling - Catches and processes all Laravel exceptions
- π·οΈ Smart Error Categorization - Automatically tags errors by type, status, environment
- π Request Context Capture - Logs complete request information including headers, parameters, and user context
- β‘ Performance Metrics - Tracks response time and memory usage for each error
- π Data Sanitization - Automatically removes sensitive data (passwords, tokens, API keys)
- π« Filtered Stack Traces - Removes sensitive function arguments from traces
- π£οΈ Path Sanitization - Strips system paths from file references
- π‘οΈ Header Protection - Redacts authentication and session headers
- π Real-time Search - Powerful Elasticsearch queries for error analysis
- π Error Trends - Track error patterns over time
- π₯ User Impact Analysis - Identify which users are affected by errors
- π Environment Comparison - Compare error rates across environments
- β Authentication Exceptions - Secure handling of auth failures
- β Validation Exceptions - Detailed validation error tracking
- β Model Not Found Exceptions - Resource not found handling
- β Authorization Exceptions - Permission error management
- β HTTP Exceptions - HTTP status code tracking
- β Database Query Exceptions - SQL error monitoring
- β Custom Exception Support - Extensible for custom exceptions
- π PHP 8.2 or higher
- π― Laravel 11.x (compatible with 10.x)
- π Elasticsearch 8.x
- π¦ Composer for dependency management
# Install Elasticsearch package
composer require elasticsearch/elasticsearch
2. **Laravel Configuration**
- Add Elasticsearch package
- Configure environment variables
- Set up service provider
3. **Index Design**
Create an Elasticsearch index with the following structure:
```json
{
"mappings": {
"properties": {
"type": { "type": "keyword" },
"message": { "type": "text" },
"status": { "type": "integer" },
"uri": { "type": "keyword" },
"method": { "type": "keyword" },
"user": { "type": "object" },
"ip": { "type": "ip" },
"trace": { "type": "text" },
"timestamp": { "type": "date" },
"environment": { "type": "keyword" },
"file": { "type": "keyword" },
"line": { "type": "integer" },
"request_data": { "type": "object" },
"headers": { "type": "object" },
"response_time": { "type": "float" },
"memory_usage": { "type": "integer" },
"tags": { "type": "keyword" }
}
}
}ELASTICSEARCH_HOST=http://localhost:9200
ELASTICSEARCH_USERNAME=elastic
ELASTICSEARCH_PASSWORD=your_secure_password'elk' => [
'host_ip' => env('ELASTICSEARCH_HOST', 'http://localhost:9200'),
'username' => env('ELASTICSEARCH_USERNAME', 'elastic'),
'password' => env('ELASTICSEARCH_PASSWORD', ''),
],{
"_index": "error-logs",
"_type": "_doc",
"_id": "a7b8c9d0e1f2g3h4i5j6k7l8m9n0o1p2",
"_score": 1.0,
"_source": {
"id": "d5e8a7b2c9f4e6a1b3d7f8e2c5a9b4d6",
"type": "ValidationException",
"message": "The email field is required.",
"status": 422,
"uri": "/api/users",
"method": "POST",
"user": {
"id": "12345",
"type": "sanctum_user",
"name": "John Doe",
"email": "john.doe@example.com"
},
"ip": "192.168.1.100",
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"trace": "[{\"file\":\"/app/Http/Controllers/UserController.php\",\"line\":45,\"function\":\"validate\",\"class\":\"Illuminate\\\\Http\\\\Request\"},{\"file\":\"/app/Http/Controllers/UserController.php\",\"line\":23,\"function\":\"store\",\"class\":\"App\\\\Http\\\\Controllers\\\\UserController\"},{\"file\":\"/vendor/laravel/framework/src/Illuminate/Routing/Controller.php\",\"line\":54,\"function\":\"callAction\"}]",
"timestamp": "2025-06-02T14:32:15.847Z",
"environment": "production",
"file": "/app/Http/Requests/CreateUserRequest.php",
"line": 28,
"context": {
"request_id": "req_9f8e7d6c5b4a3210fedcba0987654321",
"session_id": "sess_abcd1234efgh5678ijkl9012mnop3456",
"correlation_id": "corr_xyz789abc123def456ghi789jkl012"
},
"request_data": {
"input": {
"name": "John Doe",
"phone": "+1234567890",
"password": "[REDACTED]"
},
"query": {
"source": "mobile_app",
"version": "1.2.3"
},
"files": []
},
"headers": {
"host": ["api.example.com"],
"user-agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"],
"accept": ["application/json"],
"content-type": ["application/json"],
"authorization": ["[REDACTED]"],
"x-requested-with": ["XMLHttpRequest"],
"x-api-version": ["v1"],
"cookie": ["[REDACTED]"]
},
"response_time": 245.67,
"memory_usage": 8388608,
"tags": [
"production",
"validationexception",
"post",
"client_error",
"api",
"authenticated"
]
}
}- Security
- Never log sensitive data
- Implement proper data sanitization
- Use secure authentication
- Limit stack trace information
- Filter request data
- π« Never log sensitive data
- π‘οΈ Implement proper data sanitization
- π Use secure authentication
- π Limit stack trace information
- π§Ή Filter request data
- π Implement proper indexing
- π¦ Use bulk operations when possible
- π Implement connection pooling
β οΈ Add proper error handling- π Include fallback mechanisms
- π Implement index lifecycle management
- π Set up proper monitoring
- πΎ Create backup strategies
- π Document error patterns
- π Regular security audits
Contributions are welcome! Please feel free to submit a Pull Request.
