The function log_app from wordpress wp-app.php written by Elias Torres shows how we might use file functions to keep track of problems and errors in our program by writing the error along with a timestamp to an error log.
/* * wp-app.php - Atom Publishing Protocol support for WordPress * Original code by: Elias Torres, <a href="http://torrez.us/archives/2006/08/31/491/">http://torrez.us/archives/2006/08/31/491/</a> * Modified by: Dougal Campbell, <a href="http://dougal.gunters.org/">http://dougal.gunters.org/</a> * * Version: 1.0.5-dc */ $app_logging = 0; // TODO: Should be an option somewhere $always_authenticate = 1; function log_app($label,$msg) { global $app_logging; if ($app_logging) { $fp = fopen( 'wp-app.log', 'a+'); $date = gmdate( 'Y-m-d H:i:s' ); fwrite($fp, "nn$date - $labeln$msgn"); fclose($fp); } }
The function log_app takes two parameters $label and $msg
It creates a global variable called $app_logging; Why? Why does it have to be global i.e. accessible from all other functions within the function library?
If $app_logging is true switched on then
1. Open a file called wp-app.log in append mode. (Create the file if it doesn’t exist) returning a file pointer $fp
2. Use gmdate() to return the current greenwich meantime date with the following format string ‘Y-m-d H:I:s’ . The format codes are:
a. Y = Year in 4 digit format eg 2007
b. m = month in two digit format, 01 to 12
c. d = day of year in two digit format 01 to 31
d. H = Hour of day with leading zeroes. Range is from 00 to 23
e. i = Minutes past the hour. Range is from: 00 to 59
f. s=Seconds past the minute with leading zeroes. Range is from 00 to 59
3. Write to the wp-app.log file we opened a string on a new line consisting of the date the label and the message
4. Close the file.

0 Comments.