In some cases you might want to write information to the WordPress debug log, even if it’s not technically an error. PHP has a function called error_log, that will print information to the error log for you. By default it does not print and format properly. For this reason, I recommend adding an extra functions to your  development project to handle formatting. It is a wrapper for error_log, that uses print_r() to format arrays and objects properly before logging them.

As a  developer I add it to theme’s functions.php or plugin main file.

Here is what the function looks like:

 

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 );
			}
		}
	}
}
/**
 *
 * @example write_log_extend("Log Err Test Message ", __FILE__, __LINE__);
 * @example write_log_extend("Log Err Test Message ");
 *
 */
if ( ! function_exists( 'write_log_extend' ) ) {
	function write_log_extend( $log, $lfilename = "", $lline = 0 ) {
		if ( true === WP_DEBUG ) {
			$extra_line = "";
			if ( "" !== $lfilename ) {
				$extra_line = " FILE: " . $lfilename . " ";
			}
			if ( "" !== $lline ) {
				$extra_line .= " on line " . $lline;
			}
			if ( is_array( $log ) || is_object( $log ) ) {
				error_log( print_r( $log, true ) . $extra_line );
			} else {
				error_log( $log . $extra_line );
			}
		}
	}
}