如何在Wordpress中使用error_log()?

Mic*_*nch 14 php debugging wordpress error-logging

我正在尝试使用error_log()我正在构建的自定义Wordpress插件,但出于某种原因,我不能.

当我使用error_log()我的网站时,我只是休息,但我没有看到任何错误debug.log.

我在我的wp_config.php文件中设置调试如下:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
Run Code Online (Sandbox Code Playgroud)

Stranegly,如果我error_log()在我的主题中使用,该网站不会破坏,但也没有任何输出debug.log.

我需要做什么才能error_log()在我的Wordpress插件和主题中使用?

我正在使用Wordpress 3.9.1.

Mic*_*nch 9

根据Codex,WP_DEBUG_DISPLAY默认情况下应设置为true,但似乎并非如此.

添加define('WP_DEBUG_DISPLAY', true);wp_config.php修复错误日志记录.

设置WP_DEBUG_DISPLAYfalse去除来自浏览器的错误,但允许他们在日志中输出.

似乎Wordpress要求define('WP_DEBUG_DISPLAY');将错误输出到日志,无论您将其设置为truefalse.


Orl*_*ter 5

更新2019


启用调试

您只需在以下代码中添加以下代码即可启用调试功能wp-config.php

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
Run Code Online (Sandbox Code Playgroud)

debug.log您的wp-content文件夹中将生成一个文件。


在生产环境中的使用

如果要记录错误而不在前端打印它们,请添加以下行:

define( 'WP_DEBUG_DISPLAY', false );
Run Code Online (Sandbox Code Playgroud)

这在生产环境中非常有用,因为页面访问者将无法看到您的日志。


打印错误

现在,您可以使用以下error_log功能来写入日志:

error_log( 'Hello World!' );
Run Code Online (Sandbox Code Playgroud)

或者使用以下print_r方法漂亮地打印输出:

error_log( print_r( 'Hello World!', true ) );
Run Code Online (Sandbox Code Playgroud)

专家提示:如果您正在使用bash,则可以使用 tail -f wp-content/debug.log