Reputation: 835
I am a new Laarvel User. Before I was using Codeigniter. In Codeigniter it was very easy to see the log files and execution time by doing tail -f. I saw Laravel has log files in app/storage/logs directory. The problem is that log file only give me errors it doesn't give any execution time or query times. Can someone help how can i see the execution time from command line ? I also tried Ioic-sharma profiler.
Upvotes: 1
Views: 31413
Reputation: 21
So for the query times, I do this to see the execution time in the terminal
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\DB;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if(env("APP_ENV")==="local"){
DB::listen(function ($query) {
error_log($query->sql); //for logging the actual query
error_log($query->time); //for logging the time
});
}
}
}
Now when running php artisan serve
you will see all the queries logged there.
Ps: the if();
statement is there to not log anything while not in a local env.
Upvotes: 1
Reputation: 603
Just to set the record straight, Laravel 4.1 logs all queries by default (not sure if that includes execution time in 4.1). In order to see the query log for the current request, you just needed to get the query log and add it to the Laravel log, like so:
$queries = \DB::getQueryLog(); // Get query log for this request
\Log::info($queries); // Add it to the laravel log
The only difference in 5.0 is that Laravel no longer logs queries by default (but definitely includes execution time), so you will need to add the following to the beginning of the script you want to profile:
\DB::connection()->enableQueryLog();
Note: The backslashes aren't necessary if you are working from the root namespace or if you "use" these functions from the root namespace.
Here's how it looks in my log:
array (
0 =>
array (
'query' => 'select * from `users` where `users`.`deleted_at` is null limit 10 offset 0',
'bindings' =>
array (
),
'time' => 0.4899999999999999911182158029987476766109466552734375,
),
)
If you want to see it from the command line, just do var_dump($queries) instead of sending them to your log.
Upvotes: 0
Reputation: 7586
You wont be able to by default as Laravel's default logger setup does not support this, one reason is Laravel doesn't even record execution times to the log.
You will have to build it in yourself or find a library that does this for you. There are couple of profiler libraries out there but most do it via showing a profiler bar at the bottom of the page.
Here is a nice project that I've used before: https://github.com/barryvdh/laravel-debugbar
If you actually want to log every request's exectution time I've whipped up a crude way of doing it, add this to your global.php
file.
$timeStart = microtime(true);
App::finish(function() use ($timeStart) {
$diff = microtime(true) - $timeStart;
$sec = intval($diff);
$micro = $diff - $sec;
Log::debug(Request::getMethod() . "[" . Request::url() . "] Time: " . round($micro * 1000, 4) . " ms");
});
This could most likely be put in a better place, or atleast start the timer earlier but it is a quick example.
Upvotes: 13