- How do I install and use this middleware in a Laravel app?
- Run `composer require rtheunissen/guzzle-log-middleware`, then inject the `Logger` class into your Guzzle client or Laravel’s HTTP facade. For example, use `Http::withMiddleware(new Logger($logger))` where `$logger` is a PSR-3 logger like Monolog. The package works seamlessly with Laravel’s HTTP client.
- Does this package work with Laravel’s HTTP client facade (Http::)?
- Yes, it fully supports Laravel’s HTTP facade. Use `Http::withMiddleware(new Logger($logger))` to wrap requests with logging. This is the recommended approach for most Laravel applications to avoid modifying core Guzzle clients.
- Can I customize the log format (e.g., JSON) for better observability?
- Absolutely. Use the `setFormatter()` method to pass a custom formatter or a callable that returns formatted strings. For JSON logs, leverage Guzzle’s `MessageFormatter` or create a custom one. This is especially useful for tools like ELK or Datadog.
- What Laravel and Guzzle versions does this package support?
- The package targets Guzzle 6.x (compatible with Laravel 7+), but may require minor adjustments for Guzzle 7+. Laravel 8/9 users should verify compatibility with their Guzzle version. Check the [README](https://github.com/rtheunissen/guzzle-log-middleware) for updates.
- How do I filter logs to avoid excessive volume (e.g., skip 200 responses)?
- Use the `setLogLevel()` method with a callable to dynamically set log levels. For example, log only errors by returning `LogLevel::ERROR` for responses with status codes >= 400. This reduces noise while keeping critical logs intact.
- Will this middleware impact performance in production?
- Logging adds minimal overhead, but high-traffic apps may need optimizations. Use async logging (e.g., Monolog’s async handlers) or sample logs (e.g., log only failed requests). Avoid logging sensitive payloads like passwords to maintain performance and security.
- Can I integrate this with Laravel’s default logging (e.g., `Log::channel()`)?
- Yes, pass Laravel’s Log facade (or a Monolog instance) to the `Logger` constructor. For example, `new Logger(Log::channel('single'))` will route logs to Laravel’s default storage/logs/laravel.log or a custom channel.
- How do I log requests/responses to an external system like Datadog or Sentry?
- Configure a Monolog handler (e.g., `DatadogHandler` or `SentryHandler`) and pass it to the `Logger`. The middleware outputs structured data, making it easy to forward logs to external tools. Ensure your formatter aligns with the target system’s requirements.
- What if I need to log only specific endpoints or exclude certain requests?
- Use the `setFormatter()` callable to conditionally log requests. For example, skip logging for `/health` endpoints or redact sensitive data from payloads. This gives you fine-grained control over log granularity.
- Are there alternatives to this package for logging Guzzle requests in Laravel?
- Alternatives include custom Guzzle middleware with Monolog or Laravel’s `Log` facade, or packages like `spatie/laravel-guzzle-logging`. However, this package offers a dedicated, lightweight solution with built-in PSR-3 support and flexible formatting, making it ideal for Laravel’s ecosystem.