How To Use Log Tables in WordPress Plugins

WordPress theme code editing

For every big WordPress Development project I work on I always add a log function, and through that I can use it to display debug information while I develop, and use the same functionality to display useful information to the users of the plugin.

It helps fixing bugs, and also in many cases users grasp more of how the logic is working and what is going on simply by looking at a helpful log screen.

There are several ways of achieving this, but my preferred method is to create a separate database table for each plugin. In that table I store 3 simple pieces of information; a timestamp, the message and a priority value.

The priority value is used when displaying the log. The default value of log entries are 0, which means debug notices, etc. Use a unique id for each type of information. I use ‘1’ for positive information delivery and ’10’ will display a red notice indicating something that should be taken care of.

The Log Function

<?php
// ---------------------------
		/**
		*
		* LOG FUNCTION
		*
		*/
function log($text,$prio=0) {
    global $wpdb;
    $table_name_log = $wpdb->prefix . "internal_log";
    $text= mysql_real_escape_string($text);
    $time= date('Y-m-d H:i:s ',time());
    $wpdb->query ($wpdb->prepare("INSERT INTO `$table_name_log` (time,note,prio)
    				  VALUES (
    				  '$time',
    				  '$text',
    				  '$prio'
    				  )"));
}
//END LOG()
?>

The function is simple, and wherever I want to save some information for the user, I make a simple call such as

<?php
$this->log("Notice Text...");
?>

The priority is per default set to 0, which means I do not need to add a priority for low-importance notices.

Some of you might notice that I get the current time from PHP and store that instead of using MySQL’s “NOW()” function. The reason is simple, some server setups report different times between the PHP server and MySQL server.

That is quite annoying when you want to present the log entries such as “1 minute and 32 seconds ago:” and the two different dates does not match up properly. This alleviates that problem.

Make the user choose

I am a big fan of user customisations where it makes sense. I try to simplify my plugins as much as possible, lowering the learning curve. The same goes for logging.

During installation and tweaking it can be helpful to have more detailed logging, for example if you are using custom MySQL queries it can be helpful to keep an eye on things behind the scene.Once everything is configured however, there is no real purpose of storing tiny details, especially since that can make important notices appear further down and perhaps not get the attention it deserves.

For that reason I choose to add a check box in the settings, indicating if detailed logging is necessary or not, and every time I need to display something that is not highly important, I add a simple if-check in the code:

<?php
if ($this->options['detailedlogging']) $this->log("Something happened that I did not plan on. Take look in the code please!");
?>

How to keep the size down

Such a database table can quickly become very big, but there is a simple way to alleviate this problem.

<?php
$log_table = $wpdb->prefix . "internal_log";

$targettime = $wpdb->get_var($wpdb->prepare("SELECT `time` from `$log_table` order by `time` DESC limit 5000,1;"));

if ($targettime) {
	$success= $wpdb->query ($wpdb->prepare("DELETE from `$log_table`  where `time` < '$targettime';"));
	if ($success) {
		// Query successful
	} else {
		// Error, fix it...
	}
}
?>

Above code will make a check to get the timestamp of the 5001st log entry, and the following query will erase any entries older than that.

2 Comments

  1. hi..
    i like this article..
    nice dude..

  2. Jeff on 14/06/2010 at 03:32

    Nice article. I’ve been logging to files, but this is much easier.

Leave a Comment





Cleverplugins.com Newsletter

Articles about how to improve your WordPress website, how to get more traffic and most importantly more customers.

Subscribe

Something went wrong. Please check your entries and try again.