PHP - error_log()函数无法输出到文件

4

标题就将要做的事情都说清楚了。我已经苦苦挣扎一整天,试图让这个工作起来。我正在创建基于PHP的登录系统,需要进行一些调试。对我最有用的是能够在PHP程序的特定点将调试信息写入文件的能力。根据文档,error_log()应该能够实现这一点,但尽管我尝试了所有方法,我仍然完全没有成功。我尝试的所有内容的完整列表如下:

  • Added the following to /etc/php/7.0/apache2/php.ini

    error_reporting = E_ALL
    display_errors = On
    log_errors = On
    
  • Additionally, tried setting error_log to locations within /usr/, /var/www/http/, and /home/

  • Used ini_set() and error_reporting() to set all of those variables, including error_log from within a PHP file

  • Manually creating the files that are supposed to be written to, and setting their owning user and group to www-data and their permissions to 777

  • Last but not least, reinstalling libapache2-mod-php7.0 and php7.0, to no avail

  • Basically everything short of using my laptop to break the 3rd story window of my office building immediately prior to jumping to my prospective death

我在谷歌上找不到其他尝试的方法,所以我想问问专家们,于是我来了。如果有人能提供任何建议,将不胜感激。


它们不是被发送到系统日志中吗?您使用error_log的第三个参数$destination吗? - Robert
1
我会仔细检查 PHP 正在运行的用户和组。PHP 可能没有更新该文件的权限。 - admcfajn
@Robert,我们在/var/logs周围搜索了一圈,什么也没找到。而且我们确实使用了error_log的第三个参数。 - Albert Capp
@admcfajn 当我们运行 ps aux 命令时,PHP 显示为在 www-data 用户下运行,但是我们稍后将尝试创建一个文件来检查用户和组。 - Albert Capp
显示剩余2条评论
1个回答

2
我猜测日志被写入系统日志,因为在error_log()中你没有指定目的地。
尝试以下代码。
error_log("An error occured", 3, "/var/tmp/my-errors.log");

请确保此文件可以由php读取,无论您的配置是fpm还是www-data,您都可以使用touch创建它并通过chmod手动添加权限。
3表示目标是一个文件。
如果您使用apache,请检查httpd.conf或任何其他位置(v可能存在的位置)中ErrorLog的位置。
ErrorLog "/var/log/apache2"

然后使用 ls -lall 等命令检查该文件是否具有以下用户组。

 -rwxrwxr-x 1 www-data www-data 

应该会出现类似这样的东西。

另一个选项是在正确的php.ini文件中设置以下内容(CLI和apache有不同的php.ini文件)。

 error_log = /var/log/phperrors.log 

那么

 touch /var/log/phperrors.log
 chown www-data: /var/log/phperrors.log
 chmod +rw /var/log/phperrors.log

虽然没有什么奇迹,但如果仍然无法正常工作,您可以使用set_error_handler()编写并注册自己的错误处理程序。您可以在php手册中找到如何操作的示例。这更像是一种hack,但肯定会起作用。如果它不起作用,则意味着根本没有触发错误,那么您应该查看是否编辑了正确的php.ini或在触发错误之前使用了ini_set()。


我的错误,这是我忘记在最初的问题中添加的一件事。我们也尝试过这个,但结果是相同的。 - Albert Capp

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