⬆️ Go to main menu ⬅️ Previous (Factories) ➡️ Next (API)
- Logging with parameters
- Log Long Running Laravel Queries
- Benchmark class
- More convenient DD
- Log with context
- Quickly output an Eloquent query in its SQL form
- Log all the database queries during development
- Discover all events fired in one request
You can write Log::info()
, or shorter info()
message with additional parameters, for more context about what happened.
Log::info('User failed to login.', ['id' => $user->id]);
DB::enableQueryLog();
DB::whenQueryingForLongerThen(1000, function ($connection) {
Log::warning(
'Long running queries have been detected.',
$connection->getQueryLog()
);
});
Tip given by @realstevebauman
In Laravel 9.32 we have a Benchmark class that can measure the time of any task.
It's a pretty useful helper:
class OrderController
{
public function index()
{
return Benchmark::measure(fn () => Order::all()),
}
}
Tip given by @mmartin_joo
Instead of doing dd($result)
you can put ->dd()
as a method directly at the end of your Eloquent sentence, or any Collection.
// Instead of
$users = User::where('name', 'Taylor')->get();
dd($users);
// Do this
$users = User::where('name', 'Taylor')->get()->dd();
New in Laravel 8.49: Log::withContext()
will help you to differentiate the Log messages between different requests.
If you create a Middleware and set this context, all Log messages will contain that context, and you'll be able to search them easier.
public function handle(Request $request, Closure $next)
{
$requestId = (string) Str::uuid();
Log::withContext(['request-id' => $requestId]);
$response = $next($request);
$response->header('request-id', $requestId);
return $response;
}
If you want to quickly output an Eloquent query in its SQL form, you can invoke the toSql() method onto it like so
$invoices = Invoice::where('client', 'James pay')->toSql();
dd($invoices)
// select * from `invoices` where `client` = ?
Tip given by @devThaer
If you want to log all the database queries during development add this snippet to your AppServiceProvider
public function boot()
{
if (App::environment('local')) {
DB::listen(function ($query) {
logger(Str::replaceArray('?', $query->bindings, $query->sql));
});
}
}
Tip given by @mmartin_joo
If you want to implement a new listener to a specific event but you don't know its name, you can log all events fired during the request.
You can use the \Illuminate\Support\Facades\Event::listen()
method on boot()
method of app/Providers/EventServiceProvider.php
to catch all events fired.
Important: If you use the Log
facade within this event listener then you will need to exclude events named Illuminate\Log\Events\MessageLogged
to avoid an infinite loop.
(Example: if ($event == 'Illuminate\\Log\\Events\\MessageLogged') return;
)
// Include Event...
use Illuminate\Support\Facades\Event;
// In your EventServiceProvider class...
public function boot()
{
parent::boot();
Event::listen('*', function ($event, array $data) {
// Log the event class
error_log($event);
// Log the event data delegated to listener parameters
error_log(json_encode($data, JSON_PRETTY_PRINT));
});
}
Tip given by @MuriloChianfa