Created
March 1, 2013 12:36
-
-
Save tareq1988/5064359 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( !function_exists( 'wp_log' ) ) { | |
/** | |
* A file based logging utility. | |
* | |
* Made for WordPress, but can be used anywhere with a single change. | |
* | |
* @author Tareq Hasan <tareq@wedevs.com> | |
* | |
* @param string $type type of the log | |
* @param string $message log message | |
* @param mixed $var any variable to log | |
*/ | |
function wp_log( $type = '', $message = '', $var = null ) { | |
if ( $var !== null ) { | |
if ( is_array( $var ) ) { | |
//replace new line with space, double space with single space | |
$message .= str_replace( array("\n", ' '), array('', ' '), var_export( $var, true ) ); | |
} elseif ( is_object( $var ) ) { | |
//prettyfy json | |
$message .= str_replace( array('":', ',', '"'), array(' => ', ', ', ''), json_encode( $var, true ) ); | |
} elseif ( is_bool( $var ) ) { | |
$message .= $var ? 'TRUE' : 'FALSE'; | |
} else { | |
$message .= $var; | |
} | |
} | |
$log_message = sprintf( "[%s][%s] %s\n", date( 'd.m.Y h:i:s' ), $type, $message ); | |
error_log( $log_message, 3, WP_CONTENT_DIR . '/my-debug.log' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment