error_log(message, type [, destination [, extra_headers ]]);
The first parameter is the error message. The second parameter
specifies where the error is logged: a value of 0
logs the error via PHP's standard error-logging
mechanism; a value of 1 emails the error to the
destination address, optionally adding any
extra_headers to the message; a value of
3 appends the error to the
destination file.
To save an error using PHP's logging mechanism, call
error_log( ) with a type of 0.
By changing the value of error_log in your
php.ini file, you can change which file to log
into. If you set error_log to
syslog, the system logger is used instead. For
example:
error_log('A connection to the database could not be opened.', 0);
To send an error via email, call error_log( ) with
a type of 1. The third parameter is the email
address to which to send the error message, and an optional fourth
parameter can be used to specify additional email headers.
Here's how to send an error message by email:
error_log('A connection to the database could not be opened.', 1, 'errors@php.net');
Finally, to log to a file, call error_log( ) with
a type of 3. The third parameter specifies the
name of the file to log into:
error_log('A connection to the database could not be opened.', 3, '/var/log/php_
errors.log');
Example 13-5. Log-rolling error handler
function log_roller($error, $error_string) {
$file = '/var/log/php_errors.log';
if(filesize($file) > 1024) {
rename($file, $file . (string) time( ));
clearstatcache( );
}
error_log($error_string, 3, $file);
}
set_error_handler('log_roller');
for($i = 0; $i < 5000; $i++) {
trigger_error(time( ) . ": Just an error, ma'am.\n");
}
restore_error_handler( );
Generally, while you are working on a site, you will want errors
shown directly in the pages in which they occur. However, once the
site goes live, it doesn't make much sense to show
internal error messages to visitors. A common approach is to use
something like this in your php.ini file once
your site goes live:
display_errors = Off
log_errors = On
error_log = /tmp/errors.log
This tells PHP to never show any errors, but instead to log them to
the location specified by the error_log directive.