Cách log câu querry và stack method trong wordpress

vào {$root}/wp-config.php thêm vào defin(‘SAVEQUERIES, true’)

sau đó vào wp-content/plugins/xxx/xxx-function.php và thêm vào cuối file script sau:

add_action( ‘shutdown’, function () {

global $wpdb;

$log_stack = true;

$log_file = fopen( ABSPATH . ‘/wp-content/sql.log’, ‘a’ );

fwrite( $log_file, PHP_EOL . PHP_EOL . “############################################################” . PHP_EOL . PHP_EOL . date( “F j, Y, g:i:s a” ) . PHP_EOL );

foreach ( $wpdb->queries as $query ) {

  fwrite( $log_file, $query[0] . ” – ($query[1] s)” );

  if ( $log_stack ) {

   fwrite( $log_file, PHP_EOL . “[Stack]: $query[2]” . PHP_EOL . PHP_EOL );

  } else {

   fwrite( $log_file, PHP_EOL . PHP_EOL );

  }

}

fclose( $log_file );

} );

Leave a Reply

You must be logged in to post a comment.