如何将PHP错误写入stderr?

6

我该如何更改php-fpm.conf(或php.ini)以便所有错误/警告/...都写入stderr(或替代stdout)?

我使用daemonize = no运行php5-fpm,希望在终端中看到所有错误。

3个回答

8
解决方法是设置...
; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Note: on highloaded environement, this can cause some delay in the page
; process time (several ms).
; Default Value: no
catch_workers_output = yes

1

是的,可以不混乱 .ini 配置文件而实现。


使用一个Unix描述符,例如以下示例之一:

php myscript.php 2> errors.log

或者使用哈希符号,文件必须设置为可执行:

./myscript 2> errors.log

从那里开始,所有的错误都将被记录。


要仅在 errors.log 中写入内容,请在您的 PHP 脚本中任何位置使用 fwrite:
<?php
fwrite(STDERR, "Some debug infos\n");

只出现在errors.log中。编写快速刷新脚本时非常有用。

请注意,重新启动时所有错误都会被清除。


要查看错误,请使用 tail -fwatch cat

watch cat errors.log

0

我认为你无法以我从你的问题中理解的方式实现这一点。

我将为您提供一个解决方案,以实时查看终端中的php。

步骤1 - 将所有错误写入文件

更改您的php.ini文件,将每个错误都写入文件/logs/phpLogs

步骤2 - 通过错误“实时”使用tail

从命令行运行tail命令 tail -f /logs/phpLogs

步骤3 - 可选使用error_log

使用php error_log命令 error_log(print_r(debug_backtrace(), true ))

编辑:(刚刚发现)我的答案类似于如何将错误和警告记录到文件中


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接