[英]Can laravel log sql queries after all?
In Illuminate/Database/Connection.php
I can see: 在Illuminate/Database/Connection.php
我可以看到:
/**
* Indicates whether queries are being logged.
*
* @var bool
*/
protected $loggingQueries = false;
/**
* Enable the query log on the connection.
*
* @return void
*/ public function enableQueryLog()
{
$this->loggingQueries = true;
}
/**
* Disable the query log on the connection.
*
* @return void
*/
public function disableQueryLog()
{
$this->loggingQueries = false;
}
/**
* Determine whether we're logging queries.
*
* @return bool
*/
public function logging()
{
return $this->loggingQueries;
}
/**
* Run a SQL statement and log its execution context.
*
* @param string $query
* @param array $bindings
* @param \Closure $callback
* @return mixed
*
* @throws \Illuminate\Database\QueryException
*/
protected function run($query, $bindings, Closure $callback)
{
...
$this->logQuery($query, $bindings, $time);
return $result;
}
/**
* Log a query in the connection's query log.
*
* @param string $query
* @param array $bindings
* @param float|null $time
* @return void */
public function logQuery($query, $bindings, $time = null) {
...
if ($this->loggingQueries) { $this->queryLog[] = compact('query', 'bindings', 'time');
}
}
Does this data get stored somewhere? 此数据是否存储在某处? How do I enable it globally, if so? 如果是这样,如何在全球启用它?
I would use middleware to enable it. 我会使用中间件来启用它。 This would allow you to enable with options (like only in local
environments, etc). 这将允许您启用选项(例如仅在local
环境中)。
You could gather the information with dd()
or using Log::info()
, etc. Something like: 您可以使用dd()
或使用Log::info()
等来收集信息。类似:
namespace App\Http\Middleware;
use DB;
use Log;
use Closure;
class EnableQueryLogMiddleware
{
public function handle($request, Closure $next)
{
if (env('local')) {
DB::enableQueryLog();
}
return $next($request);
}
public function terminate($request, $response)
{
// Here you can either Log it, DD, etc.
dd(DB::getQueryLog());
Log::info(DB::getQueryLog());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.