spatie/laravel-log-dumper
Adds an ld() helper to dump any values to your Laravel application log using Symfony VarDumper formatting. Log multiple arguments, choose or chain log levels (info/debug/error/etc.), and enable/disable logging when needed.
Installation:
composer require spatie/laravel-log-dumper
No additional configuration is required—just use the ld() helper function globally.
First Use Case:
Replace dd() or dump() calls with ld() to log variables to Laravel’s default log channel (e.g., single, daily, or stack).
ld($user, $request->all(), $this->someComplexObject);
Outputs structured logs with Symfony’s VarDumper formatting.
Where to Look First:
Debugging in Production:
Replace dd() in production to avoid halting execution:
// Instead of:
dd($user->roles); // Crashes app
// Use:
ld($user->roles); // Logs to file/database without stopping
Structured Logging: Log complex objects (e.g., Eloquent models, API responses) with automatic type casting:
ld([
'user' => $user->toArray(),
'request' => $request->except('password'),
'exception' => $e->getTraceAsString(),
]);
Integration with Monolog:
Leverage Laravel’s log channels (e.g., stack, slack, papertrail) by default:
// Log to a custom channel (e.g., 'error-logger'):
ld($error, channel: 'error-logger');
Conditional Logging:
Use in if blocks or middleware to log only under specific conditions:
if ($request->has('debug')) {
ld('Debug mode enabled', $request->all());
}
Testing:
Mock ld() in tests to verify log output:
$this->expectsLogs(function () {
ld('Test log entry');
});
Log Context: Attach metadata (e.g., user ID, request ID) to logs:
ld('Payment processed', context: ['user_id' => auth()->id(), 'tx_id' => $txId]);
Performance Logging: Log execution time alongside data:
$start = microtime(true);
$result = $this->processData();
ld('Processing time', microtime(true) - $start, $result);
Exception Handling:
Log exceptions with stack traces in App\Exceptions\Handler:
public function report(Throwable $e) {
ld($e, context: ['url' => request()->url()]);
}
Performance Overhead:
ld($users->all())) in loops or high-traffic endpoints.ld()->only(['id', 'name']) or chunk data.Log Channel Mismatch:
ld() defaults to Laravel’s default log channel. If using a custom channel (e.g., slack), ensure it’s configured in config/logging.php.ld($data, channel: 'slack');
Sensitive Data Leaks:
ld($request->except('password', 'api_token'));
VarDumper Limitations:
ld()->maxDepth(2) to limit recursion depth.Log Rotation:
log_max_files in config/logging.php to rotate logs.Verify Log Output:
Check logs in storage/logs/laravel.log (or your configured path). Use tail -f storage/logs/laravel.log in CLI.
Log Levels:
Override the default log level (e.g., debug) via config:
'log_dumper' => [
'level' => 'info', // or 'debug', 'error'
],
Custom Formatters: Extend the dumper’s behavior by publishing the config:
php artisan vendor:publish --provider="Spatie\LogDumper\LogDumperServiceProvider"
Then modify config/log-dumper.php to add custom formatters or filters.
Custom Log Channels:
Create a dedicated channel for ld() logs:
// config/logging.php
'channels' => [
'log_dumper' => [
'driver' => 'single',
'path' => storage_path('logs/debug.log'),
'level' => 'debug',
],
],
Then use:
ld($data, channel: 'log_dumper');
Pre-Log Hooks: Add middleware to transform data before logging:
// app/Providers/AppServiceProvider.php
public function boot() {
LogDumper::macro('sanitize', function ($data) {
return collect($data)->mapWithKeys(function ($value, $key) {
return str_contains($key, 'password') ? '[REDACTED]' : $value;
});
});
}
Usage:
ld($request->all())->sanitize();
Log to External Services: Use Monolog handlers to forward logs to services like Datadog or Sentry:
// config/logging.php
'channels' => [
'log_dumper' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\DatadogHandler::class,
],
],
Conditional Logging in Production:
Disable ld() in production via config:
// config/log-dumper.php
'enabled' => env('APP_ENV') !== 'production',
How can I help you explore Laravel packages today?