search

Home  >  Q&A  >  body text

How to log "deprecated warning" in Yii2 without showing it

<p>In order to switch from PHP 8.0 to 8.1, I encountered a lot of PHP <em>Deprecated Warnings</em> displays. I could simply turn them off, but it would be better to fix them for PHP 8.2, so I'm trying to get a silent logger for these warnings when my application runs in PHP 8.1. What settings do I need to make in Yii 2 and PHP? Is this possible? </p> <p>When I set PHP to <code>error_reporting( E_ALL );</code>, the Yii 2 application displays errors. This is not the "silent" way. </p> <p>Yii2 configuration: File<em>main.php</em></p> <pre class="brush:php;toolbar:false;">'targets' => [ [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ],</pre> <p>How do I just log these PHP warnings without stopping the code from running? </p>
P粉328911308P粉328911308444 days ago557

reply all(1)I'll reply

  • P粉546179835

    P粉5461798352023-09-06 00:08:27

    Try this:

    'log' => [
        'traceLevel' => YII_DEBUG ? 3 : 0,
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
                'categories' => ['yii\base\*'],
                'levels' => ['error', 'warning','deprecated'],
                'logFile' => '@runtime/logs/php_warnings.log',
            ],
    ],

    Now, PHP warnings will be logged without stopping code execution. However, in order to achieve a completely "silent" way of logging PHP warnings, you need to adjust the settings for PHP error reporting.

    Open your PHP configuration file (php.ini).

    Find the error_reporting directive and modify it to include E_WARNING. For example:

    error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE | E_WARNING

    By including E_WARNING, you ensure that PHP warnings are logged, but other types of errors are not.

    Save the PHP configuration file and restart your web server for the changes to take effect. With these changes, Yii2 will log PHP warnings to the specified log file while allowing execution of the code to continue. You can view the log file (php_warnings.log) to collect and handle PHP warnings.

    reply
    0
  • Cancelreply