When developing in Laravel, two tools you’ll likely use every day are:
-
Log::info()— for writing logs tostorage/logs/laravel.log -
dd()— for dumping and dying your data
These are super helpful, but Laravel also provides shorter, cleaner alternatives to save time and make your code easier to read. In this blog post, we’ll explore:
- How
Log::info()anddd()work - Laravel’s shorter syntax alternatives
- When and how to use them effectively
- Real-world examples
- Best practices for debugging in Laravel
🧠 Quick Recap: What Are Log::info() and dd()?
✅ Log::info()
This function writes messages or data into the Laravel log file:
use Illuminate\Support\Facades\Log;
Log::info('User logged in', ['id' => $user->id]);
The output appears in storage/logs/laravel.log.
✅ dd() – Dump and Die
The dd() helper (dump and die) outputs a variable’s value and immediately halts the script.
dd($user);
This is useful for quickly inspecting data during development.
✨ Shorter Alternatives You Can Use
Laravel provides cleaner and shorter syntax for both logging and dumping.
🔹 1. Shorter Way to Write Logs: logger()
Instead of:
Log::info('User created', ['user_id' => 42]);
You can write:
logger('User created', ['user_id' => 42]);
It does the same thing — logs an “info” level message.
✅ Example:
logger('Payment successful', ['amount' => $request->amount]);
No need to import Log or write the longer static method call.
🔹 2. Shorter Debugging with dump() and dd()
You already know:
dd($user); // dump and die
But for non-fatal inspection (i.e., keep the script running), use:
dump($user); // dump but continue
If you want even shorter inspection, here’s a trick:
['a' => 1, 'b' => 2, 'c' => 3]->dd();
Yes! From Laravel 8+, you can call dd() and dump() directly on collections and arrays.
✅ Examples:
collect([1, 2, 3])->dd();
collect($users)->dump();
It’s chainable and keeps your code clean!
🔥 Bonus: Short Logs in Tinker or Artisan Commands
You can quickly log data while working in Tinker or Artisan commands using:
logger('Something happened');
or:
logger(['debug' => true, 'mode' => 'test']);
This is cleaner than always importing and calling Log::info().
⚙️ Behind the Scenes: How logger() Works
The logger() helper function is defined in Laravel’s core and is equivalent to:
app('log')->info($message, $context);
So when you write:
logger('Something went wrong', ['user_id' => 1]);
It internally calls:
Log::info('Something went wrong', ['user_id' => 1]);
This means it’s not just shorter — it’s functionally identical.
🧪 Real-World Examples
✅ Logging in a Controller
Old way:
use Illuminate\Support\Facades\Log;
Log::info('Order processed', ['order_id' => $order->id]);
New way:
logger('Order processed', ['order_id' => $order->id]);
✅ Debug a Collection
$users = User::all();
$users->dd();
Instead of wrapping the collection in dd(), you can chain it — especially helpful during queries:
User::where('active', 1)->get()->dd();
✅ Dump but Continue Execution
$orders = Order::latest()->limit(10)->get();
$orders->dump();
// More code runs here...
Use dump() when you want to inspect but not break the flow.
🧼 Best Practices for Debugging
| Task | Best Tool |
|---|---|
| Quick inspection | dd(), dump() |
| Permanent logs | logger(), Log::info() |
| Logging structured data | logger($msg, [...]) |
| Inspecting collections | collect()->dd() |
| Production-level logs | Use log levels (debug, info, warning, etc.) |
📋 Laravel Log Levels (Recap)
Laravel supports various log levels:
Log::debug('Debugging...');
Log::info('General info...');
Log::warning('Warning...');
Log::error('Something broke...');
And their short equivalent using logger() with custom levels:
logger()->warning('Something might be wrong');
logger()->error('This is serious');
But the default logger() helper uses the info level.
📚 Summary Table
| Goal | Traditional Way | Shorter Laravel Way |
|---|---|---|
| Log info | Log::info(...) | logger(...) |
| Dump & Die | dd($data) | $collection->dd() |
| Dump only | dump($data) | $collection->dump() |
| Log inside commands | Log::info(...) | logger(...) |
🔒 A Word of Caution
- ❌ Never use
dd()in production — it halts execution and can leak sensitive data. - ❌ Avoid leaving debugging logs like
logger('Test')in production code. - ✅ Use environment-based conditions to avoid unnecessary logging:
if (app()->environment('local')) {
logger('Debug in local only');
}
✅ Final Tips
- Prefer
logger()for quick, readable logging. - Use collection methods like
->dd()and->dump()for elegant inspection. - Clean up all debugging tools before deploying to production.
- For deeper logs, consider logging to separate channels, use log levels, or tools like Sentry, Bugsnag, or Ray.
🧾 Conclusion
Laravel makes logging and debugging both powerful and beautiful. Whether you’re inspecting a value with dd() or writing structured logs with Log::info(), Laravel’s helper functions and expressive syntax offer shorter, cleaner ways to get the job done.
Next time you’re about to write:
Log::info('User created');
dd($user);
Try this instead:
logger('User created');
$user->dd();

Leave a Reply