如何为 PHP CLI 设置错误日志?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8954964/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:50:31  来源:igfitidea点击:

How to set error log for PHP CLI?

error-handlingphp

提问by User402841

I have a php script running just fine through Apache but it fails when running in cli, so I want to find out what is happening. For this, I'd like to see the error log, but the cli error log I set is not working. I have set this in the proper php.ini file, which is confirmed when I obtain the error log details through the command line:

我有一个 php 脚本通过 Apache 运行得很好,但是在 cli 中运行时它失败了,所以我想知道发生了什么。为此,我想查看错误日志,但是我设置的 cli 错误日志不起作用。我已经在正确的 php.ini 文件中设置了它,当我通过命令行获取错误日志详细信息时会得到确认:

$ php -i | grep error
display_errors => Off => Off
display_startup_errors => Off => Off
error_append_string => no value => no value
error_log => /var/log/php_error_log => /var/log/php_error_log
error_prepend_string => <font color=ff0000> => <font color=ff0000>
error_reporting => 30711 => 30711
html_errors => Off => Off
ignore_repeated_errors => Off => Off
log_errors => On => On
log_errors_max_len => 1024 => 1024
track_errors => Off => Off
xmlrpc_error_number => 0 => 0
xmlrpc_errors => Off => Off
suhosin.disable.display_errors => Off => Off
suhosin.sql.bailout_on_error => Off => Off

So error_logand log_errorsare both set. Hover, no logs are actually saved. Permissions are in place. So what can it be? I've read this thread: PHP CLI won't log errors, but could not find a solution in here.

所以error_loglog_errors都设置了。悬停,实际上没有保存日志。权限已到位。那么它可以是什么?我读过这个线程:PHP CLI will not log errors,但在这里找不到解决方案。

回答by lubosdz

PHP via CLI and HTTP request both use different php.ini configuration. In your case I am suspicios, if you are dealing with the php.ini used by CLI PHP. Can you try executing:

PHP 通过 CLI 和 HTTP 请求都使用不同的 php.ini 配置。在你的情况下,我很怀疑,如果你正在处理 CLI PHP 使用的 php.ini。你可以尝试执行:

$ php -i >> info.txt

This will save your phpinfo()into local file info.txt. Then open the file and find setting "Loaded Configuration File" - which should point absolute path to loaded php.ini file.

这会将您的phpinfo()保存到本地文件info.txt 中。然后打开文件并找到设置“加载的配置文件” - 它应该指向加载的 php.ini 文件的绝对路径。

If this is all OK, then simply run CLI command to test whether function error_logworks as expected. Execute via CLI:

如果一切正常,那么只需运行 CLI 命令来测试函数error_log是否按预期工作。通过 CLI 执行:

$ php -r "error_log('test error', 3, './errors.log');"

and you should find in the same directory file errors.logwith specified test error. If you cannot find it then you probably need to update your php.ini settings or set correct writing permissions, process or file owner.

并且您应该在同一目录中找到具有指定测试错误的文件errors.log。如果您找不到它,那么您可能需要更新您的 php.ini 设置或设置正确的写入权限、进程或文件所有者。

回答by Tomá? Fejfar

The logfile is probably already created by php (mod_php or php_fpm, etc.) with different user (www-data?). Therefore if you run your script as anyone else it may not have write access to the file or may not even have access to the path. You may try running it as different user

日志文件可能已经由不同用户(www-data?)的 php(mod_php 或 php_fpm 等)创建。因此,如果您像其他人一样运行脚本,它可能没有文件的写访问权限,甚至可能没有访问路径的权限。您可以尝试以不同的用户身份运行它

sudo -u www-data php your_script.php