PHP error_log 方法不会将错误写入我的错误日志文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7500532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP error_log method doesn't write errors to my error log file
提问by William Clark
The PHP error_log method does not write errors to the custom error log file. It will only write to /var/log/apache2/php_error.log
PHP error_log 方法不会将错误写入自定义错误日志文件。它只会写入 /var/log/apache2/php_error.log
Here are the logging settings in my php.ini
:
这是我的日志记录设置php.ini
:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE
display_errors = Off
log_errors = On
log_errors_max_len = 0 ; also tried with 9999999
error_log = /var/log/apache2/php_errors.log
PHP writes its errors to the regular Apache error log (/var/log/apache2/error.log
) rather than the one I specified above.
PHP 将其错误写入常规的 Apache 错误日志 ( /var/log/apache2/error.log
) 而不是我上面指定的日志。
Things I already tried:
我已经尝试过的事情:
- I stopped and restarted apache after changing the php.ini file
- The file
/var/log/apache2/php_errors.log
is 777 permissions and the file exists. - There are no other overriding php.ini files. (There is /etc/php5/cli/php.ini but I'm not using cli).
- There are no other error_log = xxx settings further down the php.ini file which could overrule the first one
- phpinfo() says error_log = /var/log/apache2/php_errors.log (i.e. the correct file), both under Local Value and Master Value
- The test script I'm using to generate errors doesn't contain any ini_set calls
- 更改 php.ini 文件后,我停止并重新启动了 apache
- 该文件
/var/log/apache2/php_errors.log
是 777 权限并且该文件存在。 - 没有其他覆盖 php.ini 文件。(有 /etc/php5/cli/php.ini 但我没有使用 cli)。
- php.ini 文件下方没有其他 error_log = xxx 设置可以推翻第一个设置
- phpinfo() 表示 error_log = /var/log/apache2/php_errors.log(即正确的文件),在本地值和主值下
- 我用来生成错误的测试脚本不包含任何 ini_set 调用
(FYI: I'm using Apache/2.2.17 and PHP/5.3.5-1ubuntu7.2 on Ubuntu 11.04)
(仅供参考:我在 Ubuntu 11.04 上使用 Apache/2.2.17 和 PHP/5.3.5-1ubuntu7.2)
What am I doing wrong?
我究竟做错了什么?
回答by newmangt
I'd like to answer this as it's a high result on Google even though it's an old question.
我想回答这个问题,因为它在谷歌上的结果很高,尽管这是一个老问题。
I solved this by adding write permissions for all users on the log directory.
我通过为日志目录上的所有用户添加写权限解决了这个问题。
In my case, the user 'http' needed to be able to write to /var/log/httpd/ so I ran
就我而言,用户 'http' 需要能够写入 /var/log/httpd/ 所以我跑了
# chmod a+w /var/log/httpd
If the file already exists, it may help to delete it first and allow Apache to create it.
如果该文件已经存在,首先将其删除并允许 Apache 创建它可能会有所帮助。
My php.ini file included the entire path, also.
我的 php.ini 文件也包含了整个路径。
error_log = /var/log/httpd/php_errors.log
回答by Eric Leschinski
You could try specifying the filename like this:
您可以尝试像这样指定文件名:
I'm using Apache 2.2.22 on Ubuntu.
我在 Ubuntu 上使用 Apache 2.2.22。
Use this PHP command:
使用这个 PHP 命令:
error_log("eric", 3, "/home/el/error.log");
The first parameter is the message to be sent to the error log. The 2nd parameter 3
means "expect a filename destination. The third parameter is the destination.
第一个参数是要发送到错误日志的消息。第二个参数的3
意思是“期望一个文件名目的地。第三个参数是目的地。
Create the file /home/el/error.log and set its ownership to this:
创建文件 /home/el/error.log 并将其所有权设置为:
el@apollo:~$ ll error.log
-rwxrwxr-x 1 www-data www-data 7 Dec 13 14:30 error.log
When the PHP interprets the error_log method, it appends the message to your file.
当 PHP 解释 error_log 方法时,它会将消息附加到您的文件中。
回答by Amadu Bah
You can set the error log file for a site in the VirtualHost site configuration file, e.g: /etc/apache2/sites-enabled/example.com.conf
:
您可以在 VirtualHost 站点配置文件中为站点设置错误日志文件,例如/etc/apache2/sites-enabled/example.com.conf
::
<VirtualHost *:80>
....
ErrorLog ${APACHE_LOG_DIR}/www.example.com-error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/www.example.com-access.log combined
</VirtualHost>
In unix system ${APACHE_LOG_DIR}
is usually /var/log/apache2
The errors and access will saved in ${APACHE_LOG_DIR}/www.example.com-error.log
and ${APACHE_LOG_DIR}/www.example.com-access.log
.
在 unix 系统${APACHE_LOG_DIR}
中/var/log/apache2
,错误和访问通常会保存在${APACHE_LOG_DIR}/www.example.com-error.log
和 中${APACHE_LOG_DIR}/www.example.com-access.log
。
回答by u1813888
remove php_errors.log and restart apache2, the server itself will create a new php_errors.log file with proper permission and owner.
删除 php_errors.log 并重新启动 apache2,服务器本身将创建一个具有适当权限和所有者的新 php_errors.log 文件。
rm -f /var/log/apache2/php_errors.log
service apache2 restart
ll /var/log/apache2/php_errors.log
回答by BArTMAN
I spent several hours today trying to figure out why this wasn't working and trying to debug code working around the fact that I could log anything to the php error log. Needless to say, I was going at a snails pace until I got the logging working.
我今天花了几个小时试图弄清楚为什么这不起作用,并试图调试代码来解决我可以将任何内容记录到 php 错误日志的事实。不用说,在我开始伐木工作之前,我一直在以蜗牛般的速度前进。
I would guess that more than 90% of the time it is a permission issue. In my case daemon was the owner and group for the log file and I had to sudo su
to root and then change the permissions of the log file. I changed to:
我猜超过 90% 的时间是权限问题。在我的情况下,守护进程是日志文件的所有者和组,我必须sudo su
root,然后更改日志文件的权限。我改为:
chmod 777 myphperror.log
chmod 777 myphperror.log
Thank God for stackoverflow.com
感谢上帝提供 stackoverflow.com