Disable E_DEPRECATED in php error log
Asked Answered
U

3

18

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
Unplug answered 11/4, 2011 at 22:10 Comment(2)
Just fix the deprecated code. ducksTsan
You're a funny, funny man, Tomalak. :PUnplug
U
28

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.

Unplug answered 13/4, 2011 at 15:31 Comment(4)
Thanks for this answer. However, error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE doesn't work. phpinfo() picks up the correct value (22519). But my log is still flooded with E_DEPRECATED. Any idea?Morphophoneme
@till you ever find your answer?Magnetics
@Magnetics It doesn't work for me either.. So far I'm getting away with: tail -f /custom/log/file | grep -v "is deprecated occured"Seattle
I concur, this does NOT work with Apache 2.4. "/var/log/apache2/error.log" is still flooded with these messages.Kirkuk
T
2

It sounds like the software itself may be setting the error level, thus overriding the setting in php.ini.

If this is true, you're SOL.


Incidentally, if you're using Cacti, see here. Even if you're not using Cacti, I think that it sums up the scenario pretty well.

Tsan answered 11/4, 2011 at 22:31 Comment(0)
O
-4

You can use the @ symbol to suppress warnings (unless a custom error handler is used), for example:

@unlink("http://something.bad/");

Or:

@require_once('abc.pphp');

Or even:

$var = @$_GET['something not set'];

That said, you must use it wisely, it can easily cause issues and it makes debugging harder.

Overriding answered 11/4, 2011 at 22:17 Comment(3)
Don't use @! Use the PHP error reporting settings to show or hide warnings. When using @ there is no notification whatsoever. Using the error reporting settings you can log all errors to the error log without showing them. This way they won't be lost.Williawilliam
Firstly, there's a pretty noticeable disclaimer in my post. Secondly, between disabling all errors and hiding just one known error that is filling logs, I'd go with the latter approach. Thirdly... you realise this is an answer I wrote 5 years ago? We've had 6 major PHP versions since then...Overriding
Sometimes @ is useful, particularly for file operations, where a command can fail no matter how pre-emptive one is. For example, one can use file_exists to check for a file before using file_get_contents, but the file could be deleted by another session between them, so an E_WARNING is issued.Henchman

© 2022 - 2024 — McMap. All rights reserved.