Skip to content

Latest commit

 

History

History
115 lines (81 loc) · 3.15 KB

log-and-debug.md

File metadata and controls

115 lines (81 loc) · 3.15 KB

Log and debug

⬆️ Go to main menu ⬅️ Previous (Factories) ➡️ Next (API)

Logging with parameters

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]);

Log Long Running Laravel Queries

DB::enableQueryLog();

DB::whenQueryingForLongerThen(1000, function ($connection) {
     Log::warning(
          'Long running queries have been detected.',
          $connection->getQueryLog()
     );
});

Tip given by @realstevebauman

Benchmark class

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

More convenient DD

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();

Log with context

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;
}

Quickly output an Eloquent query in its SQL form

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

Log all the database queries during development

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