本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/288
1.在config/database.php添加以下内容
代码语言:javascript复制'enableQueryLog' => env('ENABLE_QUERY_LOG',false),
2.打开appProvidersAppServiceProvider.PHP,在boot方法中添加如下内容
代码语言:javascript复制use IlluminateSupportFacadesLog;
use IlluminateSupportFacadesDB;
代码语言:javascript复制public function boot()
{
if (config('database.enableQueryLog')) {
DB::listen(
function ($sql) {
foreach ($sql->bindings as $i => $binding) {
if ($binding instanceof DateTime) {
$sql->bindings[$i] = $binding->format(''Y-m-d H:i:s'');
} else {
if (is_string($binding)) {
$sql->bindings[$i] = "'$binding'";
}
}
}
// Insert bindings into query
$query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql);
$query = vsprintf($query, $sql->bindings);
Log::info('查询语句耗时 - ' . $sql->time . ' 毫秒 ', [
'链接名称' => $sql->connectionName,
'Sql语句' => $query
]);
}
);
}
}
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/288