【问题标题】:Yii2: How do I debug console commands?Yii2:如何调试控制台命令?
【发布时间】:2026-01-03 22:35:02
【问题描述】:

Yii2 调试器似乎只适用于 Web 请求。如何调试控制台命令 (CLI)?

例如。我需要查看在控制台命令期间执行的 SQL 语句...

【问题讨论】:

    标签: php debugging console yii2 command-line-interface


    【解决方案1】:

    使用记录器:

    'log' => [
    'targets' => [[
        ...
    ], [
        'class' => 'yii\log\FileTarget',
        'logFile' => '@runtime/logs/profile.log',
        'logVars' => [],
        'levels' => ['profile'],
        'categories' => ['yii\db\Command::query'],
        'prefix' => function($message) {
            return '';
        }
    ]]
    

    ]

    【讨论】:

    • 另一个属性'microtime' => true, 对查看 SQL 性能问题有很大帮助。
    【解决方案2】:

    https://github.com/achertovsky/yii2-debug-cli

    我为现有的 yii2-debug 编写了扩展来实现结果。如果您发现一些问题,请使用和 lmk。

    【讨论】: