Leon Sorun çözülmüş ama kaç sorgu yapıldığını nasıl göreceğinize dair size bir örnek göstereyim:
config/logging.php içinde channels anahtarına şunu ekleyin:
'querylog' => [
'driver' => 'daily',
'path' => storage_path('logs/query.log'),
'level' => 'debug',
],
app/Listeners/LogQuery.php:
<?php
namespace App\Listeners;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\Log;
class LogQuery
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param \Illuminate\Database\Events\QueryExecuted $query
* @return void
*/
public function handle(QueryExecuted $query)
{
Log::channel('querylog')
->debug("{$query->connection->getName()}: ", [
'query' => $this->getSqlWithBindings($query),
'bindings' => $query->bindings,
'time' => $query->time . 'ms',
]);
}
/**
* Get full query with bindings
*
* @param \Illuminate\Database\Events\QueryExecuted $query
* @return string
*/
protected function getSqlWithBindings(QueryExecuted $query): string
{
$sql = str_replace(['%', '?'], ['%%', '%s'], $query->sql);
$handledBindings = array_map(function ($binding) {
return is_numeric($binding)
? $binding
: "'" . str_replace(['\\', "'"], ['\\\\', "\'"], $binding) . "'";
}, $query->connection->prepareBindings($query->bindings));
return vsprintf($sql, $handledBindings);
}
}
app/Providers/EventServiceProvider.php içinde $listen dizisine şunu ekleyin:
\Illuminate\Database\Events\QueryExecuted::class =>[
\App\Listeners\LogQuery::class
]
Artık her sorgu çalışmasında storage/logs klasörü içinde query-2020-12-26.log gibi bir dosyanın içine sorguları loglayacak. Örneğin:
[2020-12-26 10:42:38] local.DEBUG: mysql: {"query":"select * from `users` where `users`.`id` = 1 limit 1","bindings":[1],"time":"142.01ms"}
[2020-12-26 10:42:38] local.DEBUG: mysql: {"query":"select * from `users` where `email` = 'john@doe' limit 1","bindings":["john@doe"],"time":"0.78ms"}
[2020-12-26 10:42:38] local.DEBUG: mysql: {"query":"select count(*) as aggregate from `articles`","bindings":[],"time":"42.63ms"}
[2020-12-26 10:42:38] local.DEBUG: mysql: {"query":"select * from `articles` limit 15 offset 0","bindings":[],"time":"49.25ms"}
[2020-12-26 10:42:38] local.DEBUG: mysql: {"query":"select `tags`.*, `article_tag`.`article_id` as `pivot_article_id`, `article_tag`.`tag_id` as `pivot_tag_id` from `tags` inner join `article_tag` on `tags`.`id` = `article_tag`.`tag_id` where `article_tag`.`article_id` in (1)","bindings":[],"time":"0.87ms"}