Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress debugging

Tags:

How can I write debug messages from my WordPress plugin?

Debugging in WordPress describes how can I enable the wp-content/debug.log file. But how can I write to it? Is there any logging method like wp_log($msg) or something? I didn't find such.

like image 547
Don Grem Avatar asked Feb 26 '13 09:02

Don Grem


People also ask

What is WordPress debugging?

WordPress provides a debug tool to help discover what may be the cause of an error on your website. This tool can display information on your live website, however, this is not recommended if your website is live to the public. Instead, it's recommended to create a debug log file.


2 Answers

If WP_DEBUG_LOG is set to true, the error_log-INI setting is set:

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

To write to that file, you can use the 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.

like image 158
vstm Avatar answered Oct 21 '22 22:10

vstm


Here's a simple function you can use; it will only log a message if WP_DEBUG is enabled:

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( 'This is a message for debugging purposes' ); 

Which will appear in your /wp-content/debug.log as the following line:

[13-Apr-2013 20:59:17 UTC] This is a message for debugging purposes 
like image 43
rjb Avatar answered Oct 21 '22 21:10

rjb