Skip to content

Instantly share code, notes, and snippets.

@philipjohn
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipjohn/03d44e99f6be76d162bf to your computer and use it in GitHub Desktop.
Save philipjohn/03d44e99f6be76d162bf to your computer and use it in GitHub Desktop.
A useful error logging function intended for use with WordPress. Simply add to your wp-config.php, or other non-version controlled PHP file.
<?php
if ( ! defined( 'PJID' ) ) define( 'PJID', uniqid() );
function pj_error_log(){
// Get line numbers and such
$bt = debug_backtrace();
$caller = array_shift($bt);
// First we check if we're being asked to print a pretty message
if ( func_num_args() == 2 && is_string( func_get_arg(0) ) ) {
$msg = array(
PJID,
'L' . $caller['line'],
func_get_arg(0),
':',
var_export( func_get_arg(1), true ),
);
// Do the message
error_log( implode( ' ', $msg ) );
}
else {
// Otherwise, just print each message/var accordingly.
foreach ( func_get_args() as $arg ) {
// Add the ID to our message
$msg = array( PJID );
// and the line number
$msg[] = 'L' . $caller['line'];
// This is just a string, so chuck it straight out.
if ( is_string( $arg ) )
$msg[] = $arg;
else // a variable, so var_export it
$msg[] = var_export( $arg, true );
// Do the message
error_log( implode( ' ', $msg ) );
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment