rtheunissen/guzzle-log-middleware
Lightweight Guzzle middleware for logging HTTP requests and responses. Capture method, URL, headers, body, status and timing, and route logs through PSR-3/Monolog with configurable formats, levels, and filtering—ideal for debugging and auditing API traffic.
GuzzleHttp\Client or Laravel’s HTTP client facade). It integrates seamlessly into the request/response lifecycle without disrupting core Laravel architecture.Log facade, Monolog, or third-party observability stacks (e.g., Sentry, Datadog).Http::withOptions()).AppServiceProvider) or per-request (e.g., in a service method).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Performance Overhead | Logging adds I/O latency. Structured logs (e.g., JSON) may increase payload size. | Use async logging (e.g., Monolog handlers) or sample logs in production. |
| Log Volume | High-traffic apps may generate excessive logs. | Implement log filtering (e.g., skip 200 OK responses) or rate-limiting. |
| Guzzle Version Mismatch | Guzzle 6 vs. 7+ may have breaking changes. | Test with Laravel’s default Guzzle version; use guzzlehttp/guzzle:^6.5 in composer.json. |
| Log Format Inconsistency | Custom formats may break downstream tools (e.g., ELK, Splunk). | Standardize on JSON logs with consistent fields (e.g., timestamp, method, url, status). |
| Middleware Order | Incorrect placement in Guzzle’s stack may log incomplete data. | Place after auth/retries but before error handling (e.g., Http::withMiddleware()). |
Logging Destination:
storage/logs/laravel.log, a dedicated file, or an external system (e.g., ELK, Datadog)?Log Granularity:
POST/PUT?Guzzle Usage Pattern:
Existing Observability Stack:
Performance SLAs:
Http::withMiddleware()) and Guzzle’s Client directly.AppServiceProvider).Log facade or Monolog handlers (e.g., SingleLineFormatter for JSON).Sentry\Laravel\Integration), Datadog, or ELK.Http::withMiddleware() to test logging without disrupting production.auth, orders).| Component | Compatibility Notes |
|---|---|
| Laravel Version | Works with Laravel 7+ (Guzzle 6/7). Test with Laravel 8/9 for any Guzzle 7+ changes. |
| Guzzle Version | Targets Guzzle 6; may need adapter for Guzzle 7+ (e.g., guzzlehttp/guzzle:^6.5). |
| PHP Version | PHP 7.2+ (no Laravel-specific constraints). |
| Logging Libraries | Compatible with Monolog, Laravel’s Log facade, or custom PSR-3 loggers. |
| Async Queues | Safe for use in Laravel queues/jobs (no blocking I/O if logs are async). |
AppServiceProvider::boot():
use Rtheunissen\GuzzleLogMiddleware\LogMiddleware;
Http::macro('withLogging', function () {
return $this->withMiddleware(new LogMiddleware());
});
$response = Http::withLogging()->get('https://api.example.com');
$middleware = new LogMiddleware(new \Monolog\Logger('guzzle'), new \Monolog\Handler\StreamHandler(storage_path('logs/guzzle.log')));
phpunit.xml):
<env name="GUZZLE_LOG_ENABLED" value="true"/>
config/guzzle.php or environment variables for log settings.composer.json and test updates in staging.RotatingFileHandler) or cloud storage (e.g., S3).How can I help you explore Laravel packages today?