简体   繁体   English

PHP-error_log()不会打印到文件

[英]PHP - error_log() will not print to file

Title pretty much says it all. 标题几乎说明了一切。 I've been tearing my hair out trying to get this working all day. 我一直在全力以赴地努力工作。 I'm in the process of creating a PHP-based login system and I need to do some debugging. 我正在创建基于PHP的登录系统,并且需要进行一些调试。 The most useful thing to me right now would be the ability to write debugging messages to a file at certain points throughout a PHP program. 对我而言,目前最有用的是在整个PHP程序中的某些点将调试消息写入文件的功能。 Based on the documentation, it looks like this is what error_log() is supposed to do, but despite everything I've tried, I have had absolutely no success. 根据文档,这似乎应该是error_log()要做的事情,但是尽管我尝试了一切,但我还是没有成功。 Full list of everything I've tried below: 我在下面尝试过的所有内容的完整列表:

  • Added the following to /etc/php/7.0/apache2/php.ini 将以下内容添加到/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/ 此外,尝试将error_log设置为/usr//var/www/http//home/

  • Used ini_set() and error_reporting() to set all of those variables, including error_log from within a PHP file 使用ini_set()error_reporting()来设置所有这些变量,包括PHP文件中的error_log

  • 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 手动创建应该写入的文件,并将其拥有的用户和组设置为www-data并将其权限设置为777

  • Last but not least, reinstalling libapache2-mod-php7.0 and php7.0 , to no avail 最后但并非最不重要的一点是,重新安装libapache2-mod-php7.0php7.0 ,无济于事

  • 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 基本上,在跳到我预期死亡之前,除了使用笔记本电脑打破办公大楼的第三个故事窗口外,所有其他事情

I really can't find anthing else to try on Google, so I figured I'd ask the experts, and here I am. 我真的找不到其他可以在Google上尝试的东西,所以我想请教专家,就在这里。 If anyone can provide any suggestions, it would be greatly appreciated. 如果有人可以提供任何建议,将不胜感激。

I guess the logs are written to sys logs because in error_log() you didn't provide the destination. 我猜这些日志已写入sys日志,因为在error_log()中您未提供目标位置。

Try the following code 试试下面的代码

error_log("An error occured", 3, "/var/tmp/my-errors.log");

Be sure this file can be read by php either fpm or www-data depending on your configuration you can create it before with touch and add permissions manually with chmod 确保此文件可以由php fpm或www-data读取,具体取决于您的配置,您可以在触摸之前创建它,并使用chmod手动添加权限

3 means that destination is a file 3表示目标是文件

If you use apache then check in httpd.conf or any other place(v where it may be present the location of ErrorLog 如果您使用apache,请在httpd.conf或任何其他可能存在错误的地方(v所在的位置)签入

ErrorLog "/var/log/apache2"

Then check if this file has following user group with ls -la ll etc. 然后使用ls -la ll等检查该文件是否具有以下用户组。

 -rwxrwxr-x 1 www-data www-data 

Something like this should appear. 这样的东西应该出现。

The other option is to set following in proper php.ini (CLI and apache have different php.ini files) 另一个选项是在适当的php.ini中设置以下内容(CLI和apache具有不同的php.ini文件)

 error_log = /var/log/phperrors.log 

then 然后

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

There are no miracles but if it still doesn't work you can write and register your own error handler with set_error_handler() you can find examples how to do it in php manual . 没有奇迹,但是如果仍然set_error_handler()您可以使用set_error_handler()编写和注册自己的错误处理程序,您可以在php手册中找到有关如何执行此操作的示例。 It's more like hack but it will work for sure. 它更像是hack,但可以肯定地工作。 If it won't then it means that errors are not triggered at all then you should look if you edit the correct php.ini or use ini_set() before error is triggered. 如果不这样做,则意味着根本不会触发错误,那么应该在触发错误之前查看是否编辑了正确的php.ini或使用ini_set()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM