Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Created November 4, 2012 13:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thefuxia/4012007 to your computer and use it in GitHub Desktop.
Save thefuxia/4012007 to your computer and use it in GitHub Desktop.
T5 Log Queries
<?php
/**
* Plugin Name: T5 Log Queries
* Description: Writes all queries to '/query-log.sql'.
* Plugin URI: http://wordpress.stackexchange.com/a/70853/73
* Version: 2012.11.04
* Author: Thomas Scholz
* Author URI: http://toscho.de
* Licence: MIT
*/
add_filter( 'query', 't5_log_queries' );
/**
* Write the SQL to a file.
*
* @wp-hook query
* @param string $query
* @return string Unchanged query
*/
function t5_log_queries( $query )
{
static $first = TRUE;
// Change the path here.
$log_path = apply_filters(
't5_log_queries_path',
ABSPATH . 'query-log.sql'
);
$header = '';
if ( $first )
{
$time = date( 'Y-m-d H:i:s' );
$request = $_SERVER['REQUEST_URI'];
$header = "\n\n# -- Request URI: $request, Time: $time ------------\n";
$first = FALSE;
}
file_put_contents( $log_path, "$header\n$query", FILE_APPEND | LOCK_EX );
return $query;
}
@thefuxia
Copy link
Author

thefuxia commented Nov 4, 2012

Track it with tail:

$ tail -f query-log.sql -n 50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment