error_logSynopsis
Descriptionerror_log() sends error messages to a variety of handlers, including custom log files, system logs, and email addresses. The function accepts from one to four arguments. The first argument, message() , contains the error message to be logged. This is the only required argument. The log_type() argument is an integer flag that lets error_log() know where to send the error. See the following table for valid flags. If this argument is not set, a default value of 0 will be used. The use of the error_destination() and additional_headers() arguments varies based on the value of the log_type() argument. See the following table for details.
See alsoTo force an error to be generated To create custom error handlers To specify the types of errors that are reported ExampleExample 267. Basic error_log() usage // Send an error message to the php error log error_log ("Uh oh, something bad happened."); // Mail an error message somewhere error_log ( "Uh oh, something bad happened.", 1, 'admin@example.com', "Subject: Error at $PHP_SELF\nBcc: root@example.com" ); // Append an error message to a file $date_time = date ('Y-m-d H:i:s'); error_log ("[$date_time] foo without bar on line 20\n"); Example 268. Send an error message to the default PHP error log <?php // Make sure that the log_errors and error_log directives are set // in the php.ini file. They must be set for the error_log function // to be able to write errors to the PHP error log. // If they're not set, you can set them at runtime by // uncommenting the two lines below. // ini_set ('log_errors', 1); // ini_set ('error_log', 'syslog'); // You can also specify a file for the error log directive. // ini_set ('error_log', '/var/log/php_error.log'); // Try to retrieve data from a URL. // In this case, pretend that you have invested in Flag Resources stock // and want to track the fluctuations in the stock's value. $URL = 'http://finance.yahoo.com/d/quotes.csv?s=FGRa.V&f=sl1d1t1c1ohgv&e=.csv'; // Try to open a connection to the resource. // If a connection cannot be opened, log an error to the PHP error log. // Suppress output of possible errors with the error control operator (@). if (! $fp = @ fopen ($URL, 'r')) { // Log the error error_log (sprintf ('Could not open %s in file %s at line %s.', $URL, __FILE__, __LINE__ - 2)); // Exit the script with an error message for the user die ("$URL could not be reached. Please try again later.<br /><br />"); } // Retrieve all of the data from the URL. // Suppress possible errors with the error control operator (@). while (! @ feof ($fp)) $stock_data .= @ fgets ($fp, 1024); // Remove any trailing and leading whitespace. // Add a newline at the end to make sure that // each new piece of stock data will get its // own line. $stock_data = trim ($stock_data) . "\n"; // Make sure that the data looks right. // It should be a comma-separated list of values. if (! ereg ('^([^,]+,)+[^,]+$', $stock_data)) { // Log the error error_log (sprintf ('Could not read data from %s in file %s at line %s.', $URL, __FILE__, __LINE__ - 2)); // Exit the script with an error message for the user. die ("$URL could not be read. Please try again later.<br /><br />"); } // Use error_log() to write out the retrieved stock data. // While this is not what the function was explicitly designed for, // this code is a lot shorter than doing the same thing with fopen/fwrite/fclose.... if (error_log ($stock_data, 3, 'FGR.aV')) print "Stock data written."; ?> Example 269. Use error_log to gracefully handle database server outages <?php ///*** NOTE: This example is geared towards UNIX-like systems ***/// // Configure PHP's error logging support. // Ideally, these directives should be set in the php.ini file! ini_set ('track_errors', 1); ini_set ('log_errors', 1); ini_set ('error_log', '/var/log/php_error.log'); // Grab the path to the error log. $error_log = ini_get ('error_log'); // Try to connect to a MySQL database. // Suppress possible errors with the error control operator (@). $db_link = @ mysql_connect ('localhost', 'foo', 'bar'); // Having the database server go down can cripple a dynamic web site. // Let's try to handle the problem gracefully. if (! $db_link) { // Capture the last error message generated $error = $php_error_msg; // Log the error error_log ($error); // Mail the system administrator, however... // if the site is busy, we don't want to clog the administrator's mailbox. // Let's add a bit of functionality to limit how much mail is generated. // Find the last entry containing the error message stored in $error. // Use a system command - the built-in system utilities should be much // faster at searching a large text file than PHP. // Use the exec() function to run the system command. // exec() returns the last line of output from the command. // Use escapeshellarg() to make the error message safe to use // as an argument for a shell command. $last_error = exec ('tail --lines=1000 | grep ' . escapeshellarg ($error)); // If there is no error message that matches $error within the last 15 minutes // or 1000 errors, mail the sysadmin. if (! trim ($last_error) || time() < strtotime (substr ($last_error, 1, 20)) + 15 * 60) { $time = date ('Y-m-d H:i:s'); error_log ( "The MySQL server may be down. At $time, script $PHP_SELF encountered this error: $error.", 1, $SERVER_ADMIN ); } // Route the client to an error handling page header ('Location: http://www.some.host.com/mysql_error.php'); } ?>
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|