Disable E_DEPRECATED in php error log
When debugging a legacy application on a production server, you often find yourself in a contradictory position: you need to keep log_errors = On to track down elusive runtime issues, but you absolutely cannot afford to have your error log flooded with deprecation notices from obsolete third-party libraries. This is a common scenario when upgrading codebases isn't an immediate option.
Consider the specific constraints of a server running PHP 5.3.2 on Ubuntu 10.04.2 LTS. In this environment, display_errors is set to Off, which successfully hides warnings and notices from the browser output. However, simply adjusting the error_reporting directive in php.ini often fails to stop these messages from being written to the log file. It appears that PHP ignores the error_reporting filter when determining what gets logged if log_errors is enabled. The result is a bloated log file filled with E_DEPRECATED notices, making it difficult to spot critical errors like fatal exceptions or parse errors.
The standard approach of setting error_reporting(E_ALL & ~E_DEPRECATED) in the script or .htaccess might seem sufficient, but on older PHP versions like 5.3.2, this can sometimes be unreliable depending on how the error handler is registered. A more robust method that works regardless of the error_reporting level involves using a custom error handler to filter out specific types before they reach the logger.
You can achieve this by creating a custom error handler in your application's bootstrap file or an early-loaded include:
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
// Ignore E_DEPRECATED notices
if ($errno === E_USER_DEPRECATED || $errno === E_DEPRECATED) {
return true;
}
// Let the default handler deal with other errors (which will log them)
return false;
});
However, there is a crucial nuance here regarding how PHP determines what to log. When log_errors is enabled, PHP's internal error logging function checks the error_reporting mask against the current E_ALL constant. If you want to suppress logging entirely for specific error types without disabling file logging globally, relying solely on the error handler might not prevent the internal logger from firing if the configuration is strict.
To effectively filter the log output on PHP 5.3.2, you need to ensure that the error reporting level explicitly excludes deprecations before PHP attempts to log them. While set_error_handler catches the event, the logging mechanism itself often runs independently based on the global error_reporting setting. Therefore, the most reliable configuration for this specific version is to adjust the error_reporting directive in your php.ini or environment-specific configuration file to exclude deprecated errors at the source:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
log_errors = On
This approach ensures that when PHP evaluates whether to log an error, it first checks the mask. If E_DEPRECATED is stripped from the mask, the internal logger will skip writing that specific entry to the file, even though log_errors remains active for other issues. This preserves your logging capabilities for actual bugs while keeping the deprecation noise out of your logs.
If you are using a framework or have complex autoloaders where modifying php.ini directly is difficult, remember that older PHP versions like 5.3.2 do not support all modern configuration nuances. You must ensure that the configuration change takes effect before any code executes. Restarting the web server (e.g., apache2 or nginx) after modifying php.ini is mandatory for these changes to apply.
It is also worth noting that ignoring deprecation notices is a temporary mitigation strategy. While this solves the immediate problem of log bloat, it means your application will eventually break once PHP updates and removes support for those deprecated functions. For a production server running commercial software where upgrades are not immediately available, this filtering method provides a necessary bridge until the vendor releases a compatible update or you can migrate to a supported PHP version with better deprecation handling mechanisms.
For modern Laravel applications or projects using similar PHP stacks, the ecosystem has evolved significantly since PHP 5.3.2. If you have migrated to a newer stack, you might find that composer autoloaders and framework-specific configurations handle error reporting more gracefully. Resources like https://laravelcompany.com often discuss best practices for managing error logging in modern Laravel applications, where you can leverage the built-in exception handling system alongside filtered error logs to maintain clean production environments.