38

概括

令人惊讶的是,我在 Google 或 SO 上一无所获。当我在 PHP 中抛出异常时,它会在我的控制台中出现两次,并带有错误消息和堆栈跟踪。第一次打印时显示“PHP 致命错误:...”,第二次打印时仅显示“致命错误:...”。我还没有测试过这是 Apache 插件版本。

例子

为安全起见,使用“...”缩短了一些命名空间和路径:

$ php 代码/com/.../tabular_data.php
PHP 致命错误:在 /home/codemonkey/.../tabular_data.php:56 中带有消息“不支持文件类型”的未捕获异常“异常”
堆栈跟踪:
#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 {主要}
  在第 56 行的 /home/codemonkey/.../tabular_data.php 中抛出

致命错误:/home/codemonkey/.../tabular_data.php:56 中未捕获的异常“异常”和消息“不支持文件类型”
堆栈跟踪:
#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 {主要}
  在第 56 行的 /home/codemonkey/.../tabular_data.php 中抛出

问题

我认为它与 stderr 和 stdout 都打印错误有关。无论如何,我如何很好地要求 PHP 只打印一次,最好是 stderr?


版本输出

PHP 5.3.9 (cli) (built: Jan 11 2012 17:09:48)
版权所有 (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

编码

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.

4

2 回答 2

48

Got it reproduced. The first error message is a result of the log_errors setting and goes to STDERR.

The second is a result of display_errors and goes to STDOUT.

Both settings can be altered during runtime. So in order to "ask PHP nicely", this suffices:

ini_set('log_errors', 1);
ini_set('display_errors', 0);
于 2012-01-25T12:25:05.503 回答
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:

error_log = 'd:/logs/php_error.log'

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.

于 2019-12-04T13:03:33.807 回答