For every bigger project I create 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.

SEO Booster PRO 2 is in the works! Take a look at the new generation of powerful dynamic SEO for your WordPress Blogs

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

// ---------------------------
		/**
		*
		* LOG FUNCTION
		*
		*/
function log($text,$prio=0) {
    global $wpdb;
    $table_name_log = $wpdb->prefix . "seobooster_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'
    				  )"));
    unset($query);
    unset($wpdb);
    unset($text);
    unset($time);
    unset($table_name_log);
}
//END LOG()

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

$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 customization 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, but once everything is configured, 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:

if ($this->options['detailedlogging']) $this->log("_logkeyword() qref:'$qref' - pos:$pos");

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.

$log_table = $wpdb->prefix . "seobooster_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.

Displaying the log

I keep a separate “tab” in my plugins for the log, and for SEO Booster PRO 2, I present two methods of viewing the log. The first is via AJAX, which updates every 10 seconds. No need to refresh at all.

thelog 300x126 How To Use Log Tables in WordPress Plugins

AJAX Refresh Log

Furthermore, since displaying all 5000 entries in the AJAX div would be quite hard on your server, there is also a tab named “Archives”, which displays the latest 5000 log entries.

User resetting?

It’s a very simple thing to implement, so I tend to always add a manual reset function as well. It can be useful when you have made changes, and want to view only the process from now on.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.