为什么 PHP 错误打印两次?
总结
令人惊讶的是,我在 Google 或 SO 上找不到任何关于此的信息。当我在 PHP 中引发异常时,它会在我的控制台中出现两次,并包含错误消息和堆栈跟踪。第一次打印时显示“PHP Fatal error: ...”,第二次打印时仅显示“Fatal error: ...”。我没有测试过这是 Apache 插件版本。
示例
为了安全起见,一些命名空间和路径用“...”缩短:
$ php code/com/.../tabular_data.php PHP Fatal error: Uncaught exception 'Exception' with message 'File type not supported' in /home/codemonkey/.../tabular_data.php:56 Stack trace: #0 /home/codemonkey/.../tabular_data.php(88): com\...\Tabular_Data->loadFromFile('/home/codemonke...', false) #1 /home/codemonkey/.../tabular_data.php(95): com\...\Tabular_Data::fromFile('/home/codemonke...') #2 {main} thrown in /home/codemonkey/.../tabular_data.php on line 56 Fatal error: Uncaught exception 'Exception' with message 'File type not supported' in /home/codemonkey/.../tabular_data.php:56 Stack trace: #0 /home/codemonkey/.../tabular_data.php(88): com\...\Tabular_Data->loadFromFile('/home/codemonke...', false) #1 /home/codemonkey/.../tabular_data.php(95): com\...\Tabular_Data::fromFile('/home/codemonke...') #2 {main} thrown in /home/codemonkey/.../tabular_data.php on line 56
问题
我认为它与 stderr 和 stdout 都打印错误有关。无论如何,我怎样才能很好地要求 PHP 只打印一次,最好打印到 stderr?
版本输出
PHP 5.3.9 (cli)(构建时间:2012 年 1 月 11 日 17:09:48)
版权所有 (c) 1997-2012 PHP 集团
Zend Engine v2.3.0,版权所有 (c) 1998-2012 Zend Technologies
代码
http://pastebin.com/iBUGJ2eY
这是为我显示双重异常的确切代码,其中名称空间和路径编辑为 foos。请注意,在此安装中,我总是在命令行中遇到双重异常。我几乎可以肯定问题出在 PHP 配置上。
Summary
Amazingly I could find nothing about this on Google or SO. When I throw an exception in PHP it appears in my console twice, complete with error message and stack trace. The first time it's printed it says "PHP Fatal error: ..." and the second time it just says "Fatal error: ...". I haven't tested this is the Apache plugin version.
Example
With some namespaces and paths shortened with '...' for safety:
$ php code/com/.../tabular_data.php PHP Fatal error: Uncaught exception 'Exception' with message 'File type not supported' in /home/codemonkey/.../tabular_data.php:56 Stack trace: #0 /home/codemonkey/.../tabular_data.php(88): com\...\Tabular_Data->loadFromFile('/home/codemonke...', false) #1 /home/codemonkey/.../tabular_data.php(95): com\...\Tabular_Data::fromFile('/home/codemonke...') #2 {main} thrown in /home/codemonkey/.../tabular_data.php on line 56 Fatal error: Uncaught exception 'Exception' with message 'File type not supported' in /home/codemonkey/.../tabular_data.php:56 Stack trace: #0 /home/codemonkey/.../tabular_data.php(88): com\...\Tabular_Data->loadFromFile('/home/codemonke...', false) #1 /home/codemonkey/.../tabular_data.php(95): com\...\Tabular_Data::fromFile('/home/codemonke...') #2 {main} thrown in /home/codemonkey/.../tabular_data.php on line 56
Question
I assume it has something to do with stderr and stdout both printing the error. In any case how do I ask PHP nicely to only print it once, preferably to stderr?
Version output
PHP 5.3.9 (cli) (built: Jan 11 2012 17:09:48)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
The code
http://pastebin.com/iBUGJ2eY
This is the exact code that displays double exceptions for me, with namespaces and paths edited to foos. Note that I always get double exceptions in the command line on this installation. I'm all but certain that the issue lies in the PHP configuration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
已经转载了。第一条错误消息是
log_errors
设置的结果,并转到STDERR
。第二个是
display_errors
的结果,并转到STDOUT
。这两个设置都可以在运行时更改。因此,为了“好好地询问 PHP”,这就足够了:
Got it reproduced. The first error message is a result of the
log_errors
setting and goes toSTDERR
.The second is a result of
display_errors
and goes toSTDOUT
.Both settings can be altered during runtime. So in order to "ask PHP nicely", this suffices:
就像Linus Kleen的回答中所写的那样,双重消息的原因是
display_errors
去到标准输出。我发现从 php 5.2.4 开始,当
display_errors
设置为 1 或“on”时,甚至会发生这种情况。要恢复正常行为,最好的方法是定义应存储记录的错误的文件,例如将以下内容添加
到您的 php.ini 中。
定义 error_log 文件后,您只会收到一条测试所需的消息到标准输出。
对于生产状态,您可以将
display_errors
设置为 0。Like written in the answer of Linus Kleen the reason for the double messages is that the
display_errors
go to stdout.I found out that since php 5.2.4 this even happens when
display_errors
is set to 1 or "on".To restore the normal behavoir it is the best way to define file where logged errors should be stored e.g. add this:
to your php.ini.
When the file for error_log is defined, you get only one message to stdout what you would need for testing.
And for production state you can set
display_errors
to 0.