在 php 错误日志中禁用 E_DEPRECATED
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5628148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable E_DEPRECATED in php error log
提问by Frank Koehl
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.
我有一台运行商业软件的生产服务器,该软件使用了已弃用的功能。我们已经在 php.ini 中禁用了错误输出 -- display_errors = Off
-- 所以用户看不到这些错误。但是,我们仍在记录 PHP 错误 -- log_errors = On
-- 以追踪问题。
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.
问题:PHP 似乎忽略了error_reporting
关于它最终传递给错误日志的内容的指令。无论输入什么值组合,文件记录都会发生,就像我设置为E_ALL
. 因此,我的错误日志因弃用通知而变得臃肿。
A default timezone value is set in php.ini, so timezone-related issues are not relevant.
php.ini 中设置了默认时区值,因此与时区相关的问题不相关。
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.
软件包的升级尚不可用,因此请不要建议“仅修复已弃用的代码”。我正在专门寻找方法来防止 PHP 在不完全禁用文件日志记录的情况下将弃用的错误转储到日志中。
Server details:
服务器详情:
- Ubuntu 10.04.2 LTS
- PHP 5.3.2
- Ubuntu 10.04.2 LTS
- PHP 5.3.2
回答by Frank Koehl
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 作为 Apache 模块运行时,您可以访问/更改php.ini
Apache 配置文件中using 指令中可用的任何配置设置。这些指令是...
php_value
php_flag
php_admin_value
php_admin_flag
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().
php_*
和php_admin_*
版本之间的差异是这个问题的关键。使用php_admin_value
和设置的值php_admin_flag
只能在 Apache 全局和 VirtualHost 配置中设置;它们不能被 .htaccess 或 ini.set() 覆盖。
The error_reporting()
function is equivalent to an ini_set()
call, and falls under the same rules.
该error_reporting()
函数相当于一个ini_set()
调用,并且属于相同的规则。
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
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 is22519
.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.The third line enables file logging.
Last one turns off on-page error display.
第一行是 的按位值
error_reporting = E_ALL & ~E_DEPRECATED
。我通过创建一个简单的脚本来检索这个值:ini_set("error_reporting", E_ALL & ~E_DEPRECATED); echo ini_get("error_reporting");
如果您想忽略系统通知以及弃用警报 -
error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE
- 按位值为22519
.第二行将所有 PHP 错误发送到自定义日志。默认情况下,PHP 将使用该
syslog
值,通常/var/log/apache2/error.log
或类似的值。第三行启用文件日志记录。
最后一个关闭页面错误显示。
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.
同样,操作的优先级和顺序是这里的关键。这些值取代了 中定义的值php.ini
,同时不能被应用程序或.htaccess
文件中的其他更改覆盖。
More details on changing configuration values outside php.ini can be found in the PHP documentation.
有关在 php.ini 之外更改配置值的更多详细信息,请参阅PHP 文档。
回答by Lightness Races in Orbit
It sounds like the software itself may be setting the error level, thus overriding the setting in php.ini
.
听起来软件本身可能正在设置错误级别,从而覆盖php.ini
.
If this is true, you're SOL.
如果这是真的,那么您就是 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.
顺便说一句,如果您使用 Cacti,请参阅此处。即使您没有使用 Cacti,我认为它也很好地概括了场景。
回答by Christian
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.
也就是说,您必须明智地使用它,它很容易导致问题并且使调试变得更加困难。