bit-mx/saloon-logger-plug-in
Laravel plug-in for Saloon v3 that logs HTTP requests, responses, and exceptions to the database with a shared ULID trace_id for end-to-end traceability. Includes automatic sanitization of sensitive headers/fields and simple trait-based setup.
Installation
composer require bit-mx/saloon-logger-plugin
Register the plugin in your Saloon HTTP client:
use BitMx\SaloonLoggerPlugin\SaloonLoggerPlugin;
$client = Saloon::connect()
->withPlugin(new SaloonLoggerPlugin());
First Use Case Log a request/response pair automatically:
$response = $client->send(new YourRequest());
// Logs are automatically captured via Saloon's lifecycle hooks
Where to Look First
SaloonLoggerPlugin.php
Focus on handle() and onRequestSent()/onResponseReceived() methods.config/saloon-logger.php (if provided) or default settings in the plugin class.stack (Laravel’s default). Override via:
$client->withPlugin(new SaloonLoggerPlugin(['channel' => 'single']));
Automatic Logging
The plugin hooks into Saloon’s RequestSent and ResponseReceived events, logging:
Customizing Log Format
Extend the plugin’s logger or override the formatLog() method:
$plugin = new SaloonLoggerPlugin([
'format' => function ($logData) {
return "Custom: {$logData['request']['method']} {$logData['request']['url']}";
}
]);
Conditional Logging Filter logs by HTTP method, status code, or custom logic:
$plugin = new SaloonLoggerPlugin([
'shouldLog' => fn ($request, $response) =>
$request->method() === 'POST' && $response->status() >= 400
]);
Integration with Laravel Logging
Use Laravel’s log channels (e.g., stack, single, daily):
$client->withPlugin(new SaloonLoggerPlugin([
'channel' => 'daily',
'level' => 'debug'
]));
Logging Exceptions Enable exception logging via:
$plugin = new SaloonLoggerPlugin(['logExceptions' => true]);
Log to External Services
Override the log() method to push logs to tools like Sentry, Datadog, or custom APIs:
$plugin = new SaloonLoggerPlugin([
'logger' => new class {
public function log($level, $message, array $context) {
// Send to external service
}
}
]);
Request/Response Masking Sanitize sensitive data (e.g., passwords) before logging:
$plugin = new SaloonLoggerPlugin([
'masker' => fn ($data) => str_replace('password', '[REDACTED]', $data)
]);
Performance Monitoring
Combine with Laravel’s stopwatch to track request durations:
$plugin = new SaloonLoggerPlugin([
'useStopwatch' => true
]);
Performance Overhead
shouldLog) or disable for non-critical endpoints.Sensitive Data Leaks
masker or pre-process requests to redact sensitive fields.Log Channel Misconfiguration
single channel not set up in config/logging.php).$plugin = new SaloonLoggerPlugin(['channel' => 'stack']); // Fallback to default
Event Hook Conflicts
Large Payloads
$plugin = new SaloonLoggerPlugin([
'maxResponseSize' => 1024, // Log only first 1KB
'hashLargeResponses' => true
]);
Check Plugin Registration Ensure the plugin is attached to the client:
$client->plugins(); // Verify SaloonLoggerPlugin is in the list
Inspect Log Levels
Use Laravel’s log levels (debug, info, error) to control verbosity:
$plugin = new SaloonLoggerPlugin(['level' => 'info']); // Skip debug logs
Test with a Minimal Request
Isolate issues by testing with a simple GET request:
$response = $client->send(new (\Saloon\Http\Concerns\Body)());
Enable Debug Mode Temporarily enable Saloon’s debug mode to see raw request/response data:
$client = Saloon::connect()->withDebug();
Custom Loggers
Implement BitMx\SaloonLoggerPlugin\Contracts\Logger to create a custom logger:
class MyLogger implements Logger {
public function log($level, $message, array $context) {
// Custom logic (e.g., write to a file, database, or queue)
}
}
Event Subscriptions
Extend the plugin to listen to additional Saloon events (e.g., RequestFailed):
$plugin->extend(function ($plugin) {
$plugin->onRequestFailed(fn ($request, $exception) => Log::error(
"Request failed: {$exception->getMessage()}",
['request' => $request->resolve()]
));
});
Dynamic Configuration Load settings from environment variables or config files:
$plugin = new SaloonLoggerPlugin([
'channel' => config('saloon-logger.channel', 'stack'),
'level' => env('SALOON_LOG_LEVEL', 'debug')
]);
Async Logging Offload logging to a queue for high-throughput systems:
$plugin = new SaloonLoggerPlugin([
'logger' => new AsyncLogger()
]);
How can I help you explore Laravel packages today?