Skip to content

Instantly share code, notes, and snippets.

@polevaultweb
Created September 25, 2020 09:59
Show Gist options
  • Save polevaultweb/00b0a63be84be5455d2eb521707691b4 to your computer and use it in GitHub Desktop.
Save polevaultweb/00b0a63be84be5455d2eb521707691b4 to your computer and use it in GitHub Desktop.
WordPress mu-plugin for logging stacktraces in WordPress code
<?php
// Add this file to the wp-content/mu-plugins/ directory.
// It may need to be created if it doesn't exist
/**
* These constants must be set in wp-config.php
*
* define('WP_DEBUG', true);
* define('WP_DEBUG_LOG', true);
* define('WP_DEBUG_DISPLAY', false);
*
*/
function pvw_generate_trace() {
$e = new Exception();
$trace = explode( "\n", $e->getTraceAsString() );
// reverse array to make steps line up chronologically
$trace = array_reverse( $trace );
array_shift( $trace ); // remove {main}
array_pop( $trace ); // remove call to this method
$length = count( $trace );
$result = array();
for ( $i = 0; $i < $length; $i ++ ) {
$result[] = ( $i + 1 ) . ')' . substr( $trace[ $i ], strpos( $trace[ $i ], ' ' ) ); // replace '#someNum' with '$i)', set the right ordering
}
return "\t" . implode( "\n\t", $result );
}
function pvw_trace( $message ) {
error_log( 'PVW Debug: ' . $message );
error_log( print_r( pvw_generate_trace(), true ) );
}
// Eg. place this function call somewhere in your code to see what is calling it
// The $message parameter gives context to the log if you are using it in multiple places
// pvw_trace( 'inserting default registration form' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment