How to log to a file using the Stanford Web Application Toolkit
From Web Services Wiki
Contents |
Problem
You want to log user activity or errors in a text file.
Solution
Use the StanfordLogFile class to log messages to a file. Each log entry is automatically populated with the date and time, remote host IP, script name, and query string. Log files are stored in CSV format for easy parsing.
Warning about AFS performance
AFS, the file system used at Stanford, is not suitable for reliable high-volume reading and writing of files, such as in the case of a log. For all mid-to-high traffic sites, we recommend logging to a database table. If you do not have access to a database and do not experience heavy spikes in traffic, StanfordLogFile is a suitable alternative.
Basic usage
Include the class and initialize it with the path to the log file to which to append. Call append to save a new message to the file.
// Include StanfordLogFile include_once("stanford.log.file.php"); // Create a new StanfordLogFile $logFile = new StanfordLogFile("/path/to/file.log"); // Save message $logFile->append("User updated their profile information.");
Format of the log file
Log files are saved in CSV format which allows for easy parsing. The format is as follows:
date,IP,script,query string,message
An example is shown below:
"2008-11-06 16:57:57",255.1.2.3,/~sunetid/cgi-bin/log.php,"var=test","This is an error message." "2008-11-06 16:59:33",4.5.6.255,/~sunetid/cgi-bin/log.php,"","This is a different error message." "2008-11-06 17:10:15",7.255.8.9,/~sunetid/cgi-bin/log.php,,"display=1&id=4","You should probably fix this."
This log file may be parsed, sorted, and displayed as an HTML table using StanfordTextFile. See the discussion topic below entitled "Displaying the log in an HTML table" for more information.
Sending the log via e-mail
You may configure either of the Stanford logging classes (StanfordLogFile or StanfordLogDatabase) to be sent via e-mail at a specified interval. To find out more, read How to set up an e-mail log using the Stanford Web Application Toolkit.
Truncating the log
To truncate the log to zero length, call the truncate function.
// Truncate the log $log->truncate();
Adding logging capabilities to StanfordApp
You may attach a StanfordLog object to StanfordApp by calling the function set_logger, as shown below. Then, you may call $app->log() in the same manner that you would call $log->append(). Using StanfordApp to manage logging may be desirable for aesthetic purposes and storage type independence, since it is easy to swap out a different type of logging object if a change is necessary in the future.
// Include StanfordApp and StanfordLogFile include_once("stanford.app.php"); include_once("stanford.log.file.php"); // Create a new app $app = new StanfordApp(); // Create a new log $log = new StanfordLogFile("/path/to/file.log"); // Attach logger to app $app->set_logger($log); // Append a message to the log file $app->log("a message");
To get the logging object from StanfordApp, call get_logger.
// Get the StanfordLog object $log = $app->get_logger();
Discussion
Displaying the log in an HTML table
The script below displays the 20 most recent log entries in a simple HTML table. For more information on StanfordTextFile, read How to parse, sort, and display a text file using the Stanford Web Application Toolkit.
// Include StanfordTextFile include_once("stanford.textfile.php"); // Create new StanfordTextFile and point it to the log file $data = new StanfordTextFile("/path/to/file.log"); // Set file has headings to false since the log file does not have headings on the first line $data->set_file_has_headings(false); // Set field names (each parameter corresponds, in order, to the columns in the log file) $data->set_field_names("log_time","remote_addr","script","query_string","message"); // Sort by timestamp, newest first $data->sort_by("log_time", StanfordData::DESCENDING); // Limit to 20 $data->set_limit(20); // Display table $data->display_as_html_table();

