我的 WordPress debug.log 充满了这组七个 PHP 警告/通知,至少每半小时重复一次……
[08-Mar-2018 09:05:03 UTC] PHP Notice: Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3736
[08-Mar-2018 09:05:03 UTC] PHP Notice: Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3738
[08-Mar-2018 09:05:03 UTC] PHP Notice: Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3740
[08-Mar-2018 09:05:03 UTC] PHP Notice: Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3736
[08-Mar-2018 09:05:03 UTC] PHP Notice: Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3738
[08-Mar-2018 09:05:03 UTC] PHP Notice: Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 3740
[08-Mar-2018 09:05:03 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/wp-includes/class-wp-query.php:3736) in /home/mysite/public_html/wp-includes/pluggable.php on line 1216
问题是,据我所知,没有提到哪个插件或罪魁祸首导致了这个(我认为这个术语是,没有回溯/堆栈跟踪),所以我发现很难调试。
问题是: 我怎样才能找到更多细节以便追查原因?
我已经有了 WP_DEBUG
, WP_DEBUG
和 WP_DEBUG_DISPLAY
全部设置为真。
我已经研究过设置 PHP display_errors
和 error_reporting
但是,由于我已经收到通知和警告输出,我不确定这些设置是否会添加更多详细信息。
这些警告是否可能是由非 WordPress 插件生成的? 我确实有一个在 cron 上运行的 PHP 脚本,它调用 wpdb 环境,但严格来说它不是一个插件。
您可以通过捕获 E_NOTICE 消息来获取回溯。 如果您将以下代码片段添加到 wp-config.php 之前
/* That's all, stop editing! Happy blogging. */
您应该在错误日志中看到完整的回溯。
set_error_handler(function() {
error_log(print_r(debug_backtrace(), true));
return true;
}, E_USER_NOTICE);
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
/* That's all, stop editing! Happy blogging. */
PS:回归 true
函数末尾的 告诉 PHP 停止处理 E_NOTICE,这可能是此时您想要的。
我 只是 在一个项目中使用了这种方法,这正是我正在寻找的解决方案。 事实上,我在寻找做同样事情的方法时发现了这篇文章。 航行愉快!
Ted 的回答对我来说非常有用,但过于冗长,既难以阅读又容易在您不小心打开时意外创建大量日志文件。
我最终将其用于更简洁的日志条目:
set_error_handler(function() {
$log = "\r\n==========\r\nSTACK TRACE:";
foreach ( debug_backtrace() as $i => $item ) {
if ( 0 === $i ) {
$log .= "\r\n" . $item['args'][1];
} else {
$log .= "\r\n" . $item['file'] . ' -- line ' . $item['line'];
}
}
$log .= "\r\n==========";
error_log( $log );
return true;
}, E_USER_NOTICE);
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
这将我正在查看的特定错误从日志文件中的 13,000 行减少到大约 20 行,并使我能够快速确定哪个插件是罪魁祸首。
