How to turn on debugging and log the messages in WordPress

You can enable WordPress debug without displaying the messages publicly to the site visitors.

You can enable WordPress debug in WordPress by adding

set_error_handler(function() {

        $log = " ******************** TRACE: ";

        foreach ( debug_backtrace() as $i => $item ) {

            if ( 0 === $i ) {

                $log .= " " . $item['args'][1];

            } else {
if(isset($item['file'])){
    $log .= " " . $item['file'] . ' — line ' . $item['line'];
}
            }

        }

        $log .= "***********************";

        error_log( $log );

        return true;

    }, E_ALL);

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define( 'WP_DEBUG_DISPLAY', false );


to the wp-config.php file. 

WordPress debug messages are logged without displaying to your site visitors.

You can find wp-config.php in your WordPress directory and copy and paste the 3 lines above in that file. Make sure none of the lines are repeated in the file.

You can then find the debug.log file in the WordPress wp-content directory.