PHP CLI 不会记录错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6387542/
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 CLI won't log errors
提问by bcmcfc
PHP currently will not log errors produced from the command line.
PHP 目前不会记录从命令行产生的错误。
I have :
我有 :
log_errors = On
error_log = /var/log/php_errors.log
in /etc/php5/cli/php.ini
在 /etc/php5/cli/php.ini
Am I missing a further setting to get this working?
我是否缺少进一步的设置来使其正常工作?
采纳答案by George Cummins
Please check that the user account running PHP CLI has write access to /var/log/php_errors.log
.
请检查运行 PHP CLI 的用户帐户是否具有对/var/log/php_errors.log
.
Additionally, you can verify that you are using the correct php.ini file like this:
此外,您可以验证您使用的是正确的 php.ini 文件,如下所示:
php -a -c /etc/php5/cli/php.ini
回答by JakeGould
This question and answer thread was very helpful to me while setting up PHP CLI logging on an Ubuntu 12.04 environment, so I wanted to post an answer that distills what I learned. In addition to the great info provided by David Chan
as well as George Cummins
I have created a logrotate.d
script to ensure the PHP CLI error log doesn't grow out of control as well as set this up so multiple users will be able to log errors to the common PHP CLI error log.
在 Ubuntu 12.04 环境中设置 PHP CLI 登录时,这个问答线程对我非常有帮助,所以我想发布一个提炼我所学知识的答案。除了所提供的巨大的信息David Chan
以及George Cummins
我创建了一个logrotate.d
脚本,以确保PHP的CLI错误日志不会增长失控以及设置这使多个用户将能够登录错误中常用的PHP CLI错误日志。
First, the default behavior of the PHP CLI is to log error messages to standard output; logging to a file is not default behavior. Which usually means logging to the same command line terminal session that is running the PHP CLI command. While the PHP ini file does have accommodations for a specified error_log
additional accommodations need to be made to truly make it work.
首先,PHP CLI 的默认行为是将错误消息记录到标准输出;记录到文件不是默认行为。这通常意味着登录到运行 PHP CLI 命令的同一个命令行终端会话。虽然 PHP ini 文件确实对指定的error_log
其他调整进行了调整,但需要进行调整才能真正使其工作。
First, I had to create an initial php_errors.log
file:
首先,我必须创建一个初始php_errors.log
文件:
sudo touch /var/log/php_errors.log
Since the server in question is used by web developers working on various projects, I have setup a common group for them called www-users
. And in this case, I want the php_errors.log
to be readable and writable by www-users
I change the ownership of the file like this:
由于有问题的服务器由从事各种项目的 Web 开发人员使用,因此我为他们设置了一个名为www-users
. 在这种情况下,我希望php_errors.log
通过www-users
像这样更改文件的所有权来可读可写:
sudo chown root:www-users /var/log/php_errors.log
And then change the permissions of the file to this:
然后将文件的权限更改为:
sudo chmod 664 /var/log/php_errors.log
Yes, from a security standpoint having a log file readable and writable by anyone in www-users
is not so great. But this is a controlled shared work environment. So I trust the users to respect things like this. And besides, when PHP is run from the CLI, any user who can do that will need write access to the logs anyway to even get a log written.
是的,从安全的角度来看,任何人都可以读写的日志文件www-users
并不是那么好。但这是一个受控的共享工作环境。所以我相信用户会尊重这样的事情。此外,当从 CLI 运行 PHP 时,任何可以执行此操作的用户都需要对日志进行写访问,甚至可以写入日志。
Next, go into /etc/php5/cli/php.ini
to adjust the default Ubuntu 12.04 settings to match this new log file:
接下来,进入/etc/php5/cli/php.ini
调整默认的 Ubuntu 12.04 设置以匹配这个新的日志文件:
sudo nano /etc/php5/cli/php.ini
Happily log_errors
is enabled by default in Ubuntu 12.04:
Happilylog_errors
在 Ubuntu 12.04 中默认启用:
log_errors = On
But to allow logging to a file we need to change the error_log
to match the new file like this:
但是要允许记录到文件,我们需要更改error_log
以匹配新文件,如下所示:
error_log = /var/log/php_errors.log
Setup a logrotate.d
script.
设置logrotate.d
脚本。
Now that should be it, but since I don't want logs to run out of control I set a logrotate.d
for the php_errors.log
. Create a file called php-cli
in /etc/logrotate.d/
like this:
现在应该是这样,但由于我不希望日志失控,所以我logrotate.d
为php_errors.log
. 创建一个名为文件php-cli
中/etc/logrotate.d/
是这样的:
sudo nano /etc/logrotate.d/php-cli
And place the contents of this log rotate daemon script in there:
并将此日志轮换守护程序脚本的内容放在那里:
/var/log/php_errors.log {
weekly
missingok
rotate 13
compress
delaycompress
copytruncate
notifempty
create 664 root www-users
sharedscripts
}
Testing the setup.
测试设置。
With that done, let's test the setup using David Chan
's tip above:
完成后,让我们使用David Chan
上面的提示测试设置:
php -r "error_log('This is an error test that we hope works.');"
If that ran correctly, you should just be bounced back to an empty command prompt since PHP CLI errors are no longer being sent to standard output. So check the actual php_errors.log
for the test error message like this:
如果运行正确,您应该会返回到一个空的命令提示符,因为 PHP CLI 错误不再发送到标准输出。因此,请检查实际php_errors.log
的测试错误消息,如下所示:
tail -n 10 /var/log/php_errors.log
And there should be a timestamped error line in there that looks something like this:
那里应该有一个带时间戳的错误行,看起来像这样:
[23-Jul-2014 16:04:56 UTC] This is an error test that we hope works.
回答by David Chan
as a diagnostic you can attempt to force a write to the error log this way.
作为诊断,您可以尝试以这种方式强制写入错误日志。
php -c /etc/php5/cli/php.ini -r " error_log('test 123'); "
you should now see test 123 in your log
您现在应该在日志中看到 test 123
tail /var/log/php_errors.log
回答by Dangelov
The logging/reporting behaviour of PHP is dependant on error_reporting too.
PHP 的日志记录/报告行为也依赖于 error_reporting。
Some PHP frameworks (for example CodeIgniter) execute an error_reporting(E_STRICT)
statement or equivalent, when in production mode, which will severely reduce the number/kind of logged errors.
某些 PHP 框架(例如 CodeIgniter)error_reporting(E_STRICT)
在生产模式下执行语句或等效语句,这将大大减少记录错误的数量/种类。
If you want to debug something, then you can just put the following statement right before your code:
如果你想调试一些东西,那么你可以在你的代码之前加上下面的语句:
error_reporting(E_ALL);
回答by Charlie
If you can't figure out what why or perhaps don't have user permissions on php.ini, another way to debug a no-information parse error is to wrap your php source in another php source with body something like:
如果您不知道为什么或可能没有 php.ini 的用户权限,另一种调试无信息解析错误的方法是将您的 php 源代码包装在另一个 php 源代码中,主体类似于:
ini_set('display_errors',1);
error_reporting(E_ALL);
include "mybustedfile.php";
回答by Saurabh Chandra Patel
in PHP file
在 PHP 文件中
error_log("You messed up!", 3, "/var/tmp/my-errors.log");
in terminal
在终端
tail -f /var/tmp/my-errors.log
outputYou messed up!
输出你搞砸了!