Laravel 5.3 - 如何记录页面上的所有查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41163199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.3 - How to log all queries on a page?
提问by DevK
My team and I are working on a rather big project. There's queries going on everywhere - in controllers, in view composers in views (lazy loading) and probably in some other services as well. It's getting hard to keep a track of it all and the page load speed is fairly slow at the moment.
我和我的团队正在开展一个相当大的项目。查询无处不在 - 在控制器中,在视图中的视图作曲家(延迟加载)以及可能在其他一些服务中。跟踪这一切变得越来越困难,目前页面加载速度相当缓慢。
Where would I put \DB::enableQueryLog() and \DB::getQueryLog() to log ALL the queries and dump them? Basically I'm looking for some place in code that happens before any of the queries happen (to put enableQueryLog()) and I'm looking for a place that happens after the views render (to dump getQueryLog()).
我会将 \DB::enableQueryLog() 和 \DB::getQueryLog() 放在哪里来记录所有查询并转储它们?基本上,我正在寻找在任何查询发生之前发生的代码中的某个位置(放置 enableQueryLog()),并且我正在寻找在视图呈现之后发生的位置(转储 getQueryLog())。
What would be a good way to go about this?
什么是解决这个问题的好方法?
Thanks in advance.
提前致谢。
回答by Hudson Pereira
Here comes the perfect example:
这是一个完美的例子:
https://laravel.com/docs/5.3/database#listening-for-query-events
https://laravel.com/docs/5.3/database#listening-for-query-events
Open app\Providers\AppServiceProvider.phpand add the following to Boot()
function:
打开app\Providers\AppServiceProvider.php并将以下内容添加到Boot()
函数中:
DB::listen(function ($query) {
var_dump([
$query->sql,
$query->bindings,
$query->time
]);
});
回答by buzkall
You can add this to the Providers/AppServiceProvider.php file and check them in the laravel log file with tail:
您可以将其添加到 Providers/AppServiceProvider.php 文件中,并在 laravel 日志文件中使用尾部检查它们:
tail -f storage/logs/laravel.log
You can even filter with queries you want to log. For example, here I was using Laravel Passport, and didn't want to log all the oauth queries.
您甚至可以使用要记录的查询进行过滤。例如,这里我使用的是 Laravel Passport,并且不想记录所有 oauth 查询。
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
public function register() {
if (App::environment('local') && env('APP_URL') == 'http://localhost') {
Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
// filter oauth ones
if (!str_contains($query->sql, 'oauth')) {
Log::debug($query->sql . ' - ' . serialize($query->bindings));
}
});
}
}
回答by Elco
Put this code right above the code where your query is executed
将此代码放在执行查询的代码上方
\DB::listen(function($sql) {
die(\Illuminate\Support\Str::replaceArray('?', $sql->bindings, $sql->sql));
});
Just modified for executable query:
刚刚修改为可执行查询:
\DB::listen(function ($query) {
// Enclose in single quotes for string params.
$bindings = collect($query->bindings)->map(function ($param) {
if(is_numeric($param)) {
return $param;
} else {
return "'$param'";
}
});
\Log::info(\Illuminate\Support\Str::replaceArray('?', $bindings->toArray(), $query->sql));
});
回答by Umang Soni
If you want to print a query which is executed on your app do following steps.
如果您想打印在您的应用程序上执行的查询,请执行以下步骤。
Step1: Go to your AppServiceProvider.php file. // File path App\Providers\AppServiceProvider.php
步骤 1:转到您的 AppServiceProvider.php 文件。// 文件路径 App\Providers\AppServiceProvider.php
Step2: Make boot() method and paste below code.
步骤 2:制作 boot() 方法并粘贴下面的代码。
public function boot() {
// Log queries
if (true) {
\DB::listen(function ($query) {
\Log::info(
$query->sql, $query->bindings, $query->time
);
});
}
}
Step3: Now you can see you queries in lumen.log or laravel.log file. File path is laravel_app\storage\logs\laravel.log or lumen.log.
第三步:现在你可以在 lumen.log 或 laravel.log 文件中看到你的查询。文件路径为 laravel_app\storage\logs\laravel.log 或 lumen.log。
Enjoy....
享受....
回答by shyammakwana.me
Additioanlly There's package available also:
另外还有可用的包:
log-my-queries
记录我的查询
https://packagist.org/packages/technoknol/log-my-queries
https://packagist.org/packages/technoknol/log-my-queries
Just install and add it's entry to middleware. It will log all the queries in laravel.log
default log file.
只需安装并将其添加到中间件即可。它将在laravel.log
默认日志文件中记录所有查询。
回答by Sherif
add a middleware that executes after the request is done and logs your queries ... see Terminable Middlwares
添加一个在请求完成后执行的中间件并记录您的查询......请参阅Terminable Middlwares
回答by Mick
Are you using MySQL? You can just tail the log.
你在使用 MySQL 吗?你可以拖尾日志。
How to show the last queries executed on MySQL?
Or use the Laravel Debug Bar?
还是使用 Laravel 调试栏?