【问题标题】:How to log db queries in Lumen5.7?如何在 Lumen5.7 中记录数据库查询?
【发布时间】:2019-04-14 10:48:59
【问题描述】:

我将 Lumen 从 5.4 升级到 5.7,我希望能够记录 DB 查询以进行调试。

这是 conf/源代码。由于第三方库的命名冲突,我必须使用“LumenDB”别名。

我希望可以记录查询,但我在 lumen.log 中看不到它们。

MyApplication.php

<?php

namespace App;

use Illuminate\Support\Facades\Facade;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class MyApplication extends \Laravel\Lumen\Application {
    public function withFacades($aliases = true, $userAliases = [])
    {
        Facade::setFacadeApplication($this);

        if (! static::$aliasesRegistered) {
            static::$aliasesRegistered = true;

            class_alias('Illuminate\Support\Facades\Auth', 'Auth');
            class_alias('Illuminate\Support\Facades\Cache', 'Cache');
            class_alias('Illuminate\Support\Facades\DB', 'LumenDB');
            class_alias('Illuminate\Support\Facades\Event', 'Event');
            class_alias('Illuminate\Support\Facades\Gate', 'Gate');
            class_alias('Illuminate\Support\Facades\Log', 'Log');
            class_alias('Illuminate\Support\Facades\Queue', 'Queue');
            class_alias('Illuminate\Support\Facades\Schema', 'Schema');
            class_alias('Illuminate\Support\Facades\Validator', 'Validator');
        }
    }

    protected function registerLogBindings()
    {
        $this->singleton('Psr\Log\LoggerInterface', function () {
            return new Logger('lumen', $this->getMonologHandler());
        });
    }

    protected function getMonologHandler()
    {
        $maxFiles = 7;
        $rotatingLogHandler = new RotatingFileHandler(storage_path('logs/lumen.log'), $maxFiles);
        $rotatingLogHandler->setFormatter(new LineFormatter(null, null, true, true));
        $handlers = [];
        $handlers[] = $rotatingLogHandler;
        return $handlers;
    }
}

bootstrap/app.php

$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);



\LumenDB::connection()->enableQueryLog();

app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\DB;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        DB::listen(function ($query) {
            // $query->sql
            // $query->bindings
            // $query->time
            Log::info("-------");
            Log::info($query->sql);
        });
    }

查询在 Service 方法中执行,该方法由 cron 调度的命令调用。

public function getAllStatsToday()
{
    $today = new \DateTime();
    $today->setTime(0, 0, 0);
    $productUsageStats = ProductUsageStat::make()
        ->where('updated_at', '>', $today)
        ->get();
    return $productUsageStats;
}

【问题讨论】:

  • 你能提供你的config/logging.php吗?您是否重新启动了应用程序?
  • @SvenHakvoort 我没有 config/logging.php,日志是在 MyApplication 中配置的(我刚刚编辑了帖子),我可以看到除了 db 日志之外的其他日志。 “重新启动应用程序”是什么意思?我只是将源文件更新到服务器上。
  • 似乎“函数 ($query)”由于某种原因从未被调用过。
  • 我将 logging.cpp 从 vendor/laravel 复制到配置,它没有帮助。

标签: php laravel laravel-5 lumen


【解决方案1】:

您尚未在您的bootstrap/app.php 中注册AppServiceProvider。因此,AppServiceProvider 中的 boot 方法永远不会被注册,因此永远不会执行日志记录。

您应该将app.php 更改为以下内容:

$app->register(App\Providers\EventServiceProvider::class);

$app->register(App\Providers\AppServiceProvider::class);

\LumenDB::connection()->enableQueryLog();

【讨论】:

  • AppServiceProvider 已注册。我错过了帖子中的那一行。
猜你喜欢
  • 2019-04-21
  • 2020-03-19
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 2014-07-15
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
相关资源
最近更新 更多