Skip to content

Instantly share code, notes, and snippets.

@tareq1988
Created March 1, 2013 12:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tareq1988/5064359 to your computer and use it in GitHub Desktop.
Save tareq1988/5064359 to your computer and use it in GitHub Desktop.
<?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