How to put logs in WordPress

Is there any way that I can log anything in WordPress similar to logs we can do it in Magento?

I am integrating a custom plugin in that I have added few functions with help of hooks, So I need to debug something in it. In this I need if I can enter any text or data into WordPress logs.

If so Please let me know the procedure for generating log into WordPress.

2

You can enable WordPress logging adding this to wp-config.php:

 // Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );

you can write to the log file using the error_log() function provided by PHP.

The following code snippet is a very useful function wrapper for it, make it available in your plugin:

if (!function_exists('write_log')) {

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

}

write_log('THIS IS THE START OF MY CUSTOM DEBUG');
//i can log data like objects
write_log($whatever_you_want_to_log);

if you cant find the debug.log file, try generating something for it, since it will not be created if there are no errors, also in some hosted servers you might need to check where the error log is located using php info.

Leave a Comment