Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging to `php artisan serve` output

Tags:

php

laravel

Is there a way for your Laravel application to log output to the standard output of the PHP development server that php artisan serve uses?

In this particular case, I want to output (Eloquent-generated) SQL queries.

like image 346
Niels B. Avatar asked Apr 03 '26 00:04

Niels B.


1 Answers

I need to be more patient with Google. I found a clue in another question and after some tweaking, I got it to behave just the way I want.

Throw this into app/start/global.php:

DB::listen(function($sql, $bindings, $time)
{
    file_put_contents('php://stdout', "[SQL] {$sql} \n" .
                      "      bindings:\t".json_encode($bindings)."\n".
                      "      time:\t{$time} milliseconds\n");
});

And php artisan serve will output SQL queries to the dev server console.

like image 182
Niels B. Avatar answered Apr 04 '26 13:04

Niels B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!