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 genrating log into wordpress.

up vote 6 down vote accepted

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, this 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.

WordPress can do logging! Check out the WordPress debugging page here https://codex.wordpress.org/Debugging_in_WordPress

I typically like to set my local development websites up to log errors in a debug file, rather than to have them output on the screen.

Head over to to your wp_config file and scroll to the bottom where it defines WP_DEBUG.

This is what my typical setup looks like:

define('WP_DEBUG', true); // To enable debugging. Leave things just like this to output errors, warnings, notices to the screen:
define( 'WP_DEBUG_LOG', true ); // To turn on logging
define( 'WP_DEBUG_DISPLAY', false ); // To prevent output of errors, warnings, notices to the screen (which I personally find SUPER annoying):

With those settings, WordPress will now log errors, warnings, and notices to a debug.log file located in /wp-content/debug.log

Log files in production environments are security threats so IF you decide to have logging on a production environment, it would be a good idea to set your .htaccess file to deny access to the log file (or similarly use a security plugin to block it). That way you still get your logs, but don't have to worry about hackers getting all that info as well.

  • Can I add any custom text in it? For example in a loop Just to get confirm which is called I want to enter numbers like 1,2,3 etc. How can I do it – Pratik bhatt Mar 16 '17 at 7:55
  • 1
    You can. Check @david's answer for how to do that :) Here's a good article on that elegantthemes.com/blog/tips-tricks/… – Ian Mar 17 '17 at 6:10

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.