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"),
        ],
    ];
}

You may have to include the debug module in your config as below:

    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        'allowedIPs' => ['192.168.10.*','192.168.1.*','127.0.0.1', '::1','*'],
    ];
1 Like

So what I ended up doing was actually simpler and, in hindsight, not sure why I didn’t do this before…

On our prod system, I made a copy of yii, named it yii-debug, and set:

defined('YII_DEBUG') or define('YII_DEBUG', true);

So if we need to debug something in prod, we just run yii-debug instead of yii. This doesn’t help with frontend debugging, but honestly that rarely comes up. For us at least, the backend code is what warrants a debug in prod.

Thanks for the input however, good to know!