Date: May 25th, 2004
Cate: Geekism
Tags:

WordPress Logging Hinters

I’ve been hacking on WordPress a bit to customize it for my needs, and I thought I’d share a few things I’ve “discovered”. These may be obvious to you, but they took me a while to figure out.

Logging

I’ve never figured out a good way to do logging from PHP. Sometimes you can get some insight into what’s going on by printing out some comments to the outgoing HTML that a view source will then reveal. But that doesn’t work in all cases: What if there’s something funny going on in the processing of a new post? There’s no place to print to! Actually, there is. Use STDERR!

$stderr = fopen('php://stderr', 'w');
fwrite($stderr,"Logging statement: value is $value\n");
fclose($stderr);

On my host, this works. There is a directory where the output from STDERR is captured. I can post a story, then look in the logfile and see what happened. It helped me debug this quite easily.

wpdb SAVEQUERIES

WordPress has the built in ability to log all the SQL queries executed while building your page, which I happened to notice while looking at the ‘wpdb’ class. Here’s how you can use it:

Step 1: Add define('SAVEQUERIES', true); to your wp-config.php. This will cause all queries run by the wpdb to be appended to an array that you can get at later.
Step 2: Print out the queries somehow. I’m printing them out in an HTML comment at the end of my main template, like this:

foreach ($wpdb->savedqueries as $q)
{
$q = trim(ereg_replace("[[:space:]]+", " ", $q));
echo "$q\n\n";
}

Now I (and you) can see exactly what queries are being executed, which may help answer some questions about what’s going on in the guts of WP.

Leave a Reply

 Name

 Mail

 Home

[Name and Mail is required. Mail won't be published.]