Enable Yii::debug in production yii2 app

There are times I’d like to see debug output on our production system. But I can’t seem to understand how to “enable” it using our config. Works just fine with on our dev system.

We do use the yii2 init to setup the dev vs prod environment. For the debug module, we don’t tweak anything beyond what is standard in the docs, and I know that init dev uses a web/index.php that has the YII_DEBUG set.

How do I safely enable debug output in certain code on a production system? Mostly, I want this when running console controllers. Is there a way to enable debug output specific to a controller using a flag? Or a setting specific to that controller?

Here is our common/main.php that is relevant to debug:

$config['components']['log']['targets'][] = [
    'class' => 'yii\log\FileTarget',
    'enabled' => true,
    'levels' => [ 'error', 'warning' ],
    'enableRotation' => true,
    'except'  => [ 'yii\web\HttpException:404' ],
    'logFile' => '@runtime/logs/errors.log',
    'logVars' => [],
    'prefix' => function ($message) {
       $userID = Yii::$app->user->id;
       return "[userID=$userID]";
    }
];

$config['components']['log']['targets'][] = [
    'class' => 'yii\log\FileTarget',
    'enabled' => true,
    'levels' => [ 'error', 'warning' ],
    'enableRotation' => true,
    'categories' => [ 'yii\web\HttpException:404' ],
    'logFile' => '@runtime/logs/404.log',
    'logVars' => [],
    'prefix' => function ($message) {
       $userID = Yii::$app->user->id;
       return "[userID=$userID]";
    }
];

$config['components']['log']['targets'][] = [
    'class' => 'yii\log\FileTarget',
    'enabled' => true,
    'enableRotation' => true,
    'except'  => [
        'yii\web\HttpException:404',
        'yii\db\*',
        'yii\base\*',
        'yii\web\UrlManager::parseRequest',
    ],
    'logFile' => '@runtime/logs/debug.log',
    'logVars' => [],
    'prefix' => function ($message) {
       $userID = Yii::$app->user->id;
       return "[userID=$userID]";
    }
];

if (! YII_DEBUG) {
    $config['components']['log']['targets'][] = [
        'class' => 'yii\log\EmailTarget',
        'mailer' => 'mailer',
        'levels' => ['error', 'warning'],
        'except'  => [ 'yii\web\HttpException:404' ],
        'message' => [
            'from' => ['xx@xx.net'],
            'to' => ['xx@xx.com'],
            'subject' => sprintf("%s - %s", gethostname(), "ERROR"),
        ],
    ];
}