简体   繁体   English

WordPress调试

[英]WordPress debugging

How can I write debug messages from my WordPress plugin?如何从我的 WordPress 插件编写调试消息?

Debugging in WordPress describes how can I enable the wp-content/debug.log file. 在 WordPress调试描述了如何启用wp-content/debug.log文件。 But how can I write to it?但是我怎么写呢? Is there any logging method like wp_log($msg) or something?有没有像wp_log($msg)类的日志记录方法? I didn't find such.我没有找到这样的。

If WP_DEBUG_LOG is set to true, the error_log -INI setting is set:如果WP_DEBUG_LOG设置为 true,则设置error_log -INI 设置

ini_set( 'error_log', WP_CONTENT_DIR . '/debug.log' );

To write to that file, you can use the error_log -function :要写入该文件,您可以使用error_log -function

error_log("This message is written to the log file");

This function is not specific to WordPress and can be used in any PHP script.此功能并非特定于 WordPress,可以在任何 PHP 脚本中使用。

Here's a simple function you can use;这是您可以使用的简单功能; it will only log a message if WP_DEBUG is enabled:如果启用了WP_DEBUG,它只会记录一条消息:

function log_me($message) {
    if ( WP_DEBUG === true ) {
        if ( is_array($message) || is_object($message) ) {
            error_log( print_r($message, true) );
        } else {
            error_log( $message );
        }
    }
}

You can call the log_me() function like this in your theme template(s):您可以在主题模板中像这样调用log_me()函数:

log_me( 'This is a message for debugging purposes' );

Which will appear in your /wp-content/debug.log as the following line:这将在您的/wp-content/debug.log显示为以下行:

[13-Apr-2013 20:59:17 UTC] This is a message for debugging purposes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM