Enable logging

WordPress has on-board logging which makes it easy during development to write messages to a log file (instead of outputting stuff to the screen).

To enable logging you need to find the following section in your wp-config.php file:

 * For developers: WordPress debugging mode.
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
define('WP_DEBUG', false);

As you can see, by default logging in turned off. To properly enable it you need to change that one line to the following:

@ini_set('display_errors', 0);
define('WP_DEBUG', TRUE);
define('WP_DEBUG_LOG', TRUE);

If you are on a UNIX like system (Linux, OSX, etc.) you will have to create the debug.log file in the wp-content directory and make sure that the web-server has write access to that file.

Now that logging is enabled, you want to write log messages. Add the following snippet to your functions.php which defines the _log() function:

if( !function_exists('_log') )
   function _log( $message ) 
      if( WP_DEBUG === TRUE )
         if( is_array( $message ) || is_object( $message ) )
            error_log( print_r( $message, TRUE ) );
            error_log( $message );

You can now write to the log file by using statements like this:

_log("This is a message.");

If you want to view the log file while your working, you might consider using tail to see new messages appear as you go along:

# tail -f debug.log

Bon App├ętit!

Tagged with: