Want to log script activity in your Web application, but haveno clue where to begin? Take our handy tour of PHP's logging functions,and find out how simple it really is.
You can also configure PHP to automatically log all script errors to a specific file via the special "log_errors" configuration directive in "php.ini". This directive, when set to true, logs all PHP errors to either a user-specified log file (the value of this file must be specified in the "error_log" configuration directive, also set via "php.ini"), or to the Web server error log if no log file is specified.
Consider the following example, which demonstrates:
<?php
// turn on automatic error logging
ini_set('log_errors', true);
// include non-existent file
include("non.existent.file.php");
?>
In this case, I'm manually instantiating an error by
attempting to include a file which does not exist. Obviously, PHP will barf and display an error screen containing the following:
Warning: Failed opening 'non.existent.file.php' for inclusion
(include_path='.;/usr/local/php/includes') in error.php on line 10
The same error also appears in the server's error log:
[Tue Nov 26 12:49:31 2002] [error] PHP Warning: Failed opening
'non.existent.file.php' for inclusion
(include_path='.;/usr/local/php/includes') in error.php on line 10
You can turn off PHP error display, and only have the error
message appear in the log file, by setting the "display_errors" variable (also accessible via "php.ini") to false. Consider the following variant of the previous example, which demonstrates:
<?php
// turn on automatic error logging
ini_set('log_errors', true);
// turn off error display
ini_set('display_errors', false);
// include non-existent file
include("non.existent.file.php");
?>
In this case, though an error takes place, it is never
displayed to the user, but merely gets logged to the server's error log.
On *NIX systems, setting the "error_log" variable to the special value "syslog" logs all errors via the standard "syslog" daemon.