【问题标题】:Can laravel log sql queries after all?laravel 到底可以记录 sql 查询吗?
【发布时间】:2017-01-08 10:55:45
【问题描述】:

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');
    }
}

这些数据是否存储在某个地方?如果是这样,我如何在全局范围内启用它?

【问题讨论】:

    标签: php sql laravel logging query-builder


    【解决方案1】:

    我会使用middleware 来启用它。这将允许您使用选项启用(例如仅在 local 环境等中)。

    您可以使用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());
        }
    }
    

    【讨论】:

    • 所以没有简单的方法,比如在config/database.php中指定一些参数?
    • 我觉得像上面这样创建中间件很容易,启用需要 DB::enableQueryLog(); anywhere,这样就可以记录查询,所以理论上你可以把它放在你的app/bootstrap/app.php文件中。不优雅,但我相信它会起作用。
    • 只是确认一下。我希望,只需更改一些配置参数即可使用它。但事实证明,它需要更多的努力。不过不多。而且我可能同意使用中间件通常是更好的选择。
    • 是的,我同意将它作为config/database.php 中的参数会很好,但目前似乎不可能。也许在未来的版本中!
    猜你喜欢
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多