【发布时间】:2019-02-11 15:13:14
【问题描述】:
我想为 PHP 和 Nginx 记录的每个错误添加 URL。由于错误日志为我提供了相关脚本,因此相同的脚本用于具有不同数据源(远程 API)的许多不同 URL。
我相信这个错误来自远程 API 的某些数据。拥有错误 URL(由用户浏览)将帮助我识别相关数据源。
Nginx
Nginx 错误日志已启用,但它只记录 Nginx 相关错误(Google Pagespeed 日志)。这很奇怪,因为在许多其他服务器上,Nginx 日志包含 Nginx 和 PHP 错误。 (log format source)
nginx.conf
log_format main '$http_x_forwarded_for - $remote_user [$time_local] '
'"$request_method $scheme://$host$request_uri $server_protocol" '
'$status $body_bytes_sent "$http_referer" ["$host" - "$request"]'
'"$http_user_agent" $request_time';
PHP
如前所述,PHP 日志包含 PHP 错误,但 nginx log_format 不适用于此。
php.ini
error_log = /var/log/php-errors.log
www.conf //I've never changed this on any server to manage logs
;php_flag[display_errors] = off
;php_admin_value[error_log] = /var/log/fpm-php.www.log
;php_admin_flag[log_errors] = on
WordPress
启用 Wordpress 调试日志似乎会停止向 PHP 错误日志输出错误。我通常禁用此功能,因为 Nginx 包含根据我的需要格式化的所有错误。
wp-config.php
define('WP_DEBUG', true);
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_DEBUG_LOG', true );
最后,我不知道如何处理所有这些不同的日志配置。
通常:
- 我的 nginx 日志包含 PHP 和 Nginx 日志,因此我可以从 nginx 配置文件管理 log_format。
提前致谢。
【问题讨论】:
-
我不知道有什么方法可以配置标准 PHP 错误日志的日志格式。我认为您可能需要为此使用自己的自定义错误处理程序。
-
谢谢。正如我所说,我通常设法让 nginx 记录所有 Nginx 和 PHP 错误日志。这意味着 PHP 错误包含在 Nginx 错误日志文件中,我可以在其上调整 log_format。
-
也许只是将 PHP 错误发送到系统的默认记录器? php.net/manual/en/errorfunc.configuration.php#ini.error-log
-
当您让 PHP 将信息传递给 syslog 时,可能是其他进程可以访问该信息并按照自己的规范对其进行格式化。
-
嗯,是的!它似乎工作!我刚刚在
php.ini中评论了error_log行。正如您链接的文档所说If this directive is not set, errors are sent to the SAPI error logger.。似乎 PHP 错误现在已记录到 Nginx 错误日志中,并尊重来自nginx.conf的 log_format。非常感谢@misorude - 我要等几分钟看看它是否正常工作。
标签: php wordpress nginx logging error-handling