Disable E_DEPRECATED in php error log

Frank Koehl picture Frank Koehl · Apr 12, 2011 · Viewed 57.3k times · Source

I have a production server running commercial software that utilizes deprecated functionality. We already disabled error outputs in php.ini -- display_errors = Off -- so users are not seeing these errors. However we are still logging PHP errors -- log_errors = On -- in order to track down issues.

The issue: PHP seems to ignore the error_reporting directive in regards to what it ultimately passes to the error log. No matter what combination of values are entered, the file logging occurs as if I'm set to E_ALL. My error log is consequently bloated with deprecation notices.

A default timezone value is set in php.ini, so timezone-related issues are not relevant.

Upgrades for the software package are not available yet, so please no recommendations to "just fix the deprecated code." I'm looking specifically for ways to prevent PHP from dumping deprecated errors into the log without disabling file logging entirely.

Server details:

  • Ubuntu 10.04.2 LTS
  • PHP 5.3.2

Answer

Frank Koehl picture Frank Koehl · Apr 13, 2011

When PHP runs as an Apache module, you can access/change any configuration setting available in php.ini using directives in Apache configuration files. Those directives are...

  • php_value
  • php_flag
  • php_admin_value
  • php_admin_flag

The difference between the php_* and php_admin_* versions is the key to this issue. Values set using php_admin_value and php_admin_flag can only be set in Apache global and VirtualHost configs; they are not overrideable by .htaccess or ini.set().

The error_reporting() function is equivalent to an ini_set() call, and falls under the same rules.

So I went into the virtualhost configuration for the site in question, and added the following lines...

php_admin_value error_reporting 22527
php_admin_value error_log /custom/log/path/php_errors.log
php_admin_flag  log_errors On
php_admin_flag  display_errors Off
  1. The first line is the bitwise value for error_reporting = E_ALL & ~E_DEPRECATED. I retrieved this value by creating a simple script:

    ini_set("error_reporting", E_ALL & ~E_DEPRECATED);
    echo ini_get("error_reporting");
    

    If you want to ignore system notices along with deprecation alerts -- error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE -- the bitwise value is 22519.

  2. The second line sends all PHP errors to a custom log. By default PHP will use the syslog value, usually /var/log/apache2/error.log or something similar.

  3. The third line enables file logging.

  4. Last one turns off on-page error display.

Again, the priority and order of operations is key here. These values supersede those defined in php.ini, and at the same time cannot be overridden by other changes within the application or in .htaccess files.

More details on changing configuration values outside php.ini can be found in the PHP documentation.