Log PHP errors with log_errors and error_logPosted May. 14, 2009 in PHP
PHP errors are by default logged to the web server's error log file (at least they are with the default setup on most Linux distros) but it is also possible to instead log PHP errors to a file of your choice. This is useful on production websites so you can periodically check for errors and fix them in a location other than the default webserver error log file.
The log_errors configuration option is a boolean option. To log to an error log file specified with the error_log configuration option set this to 1.
The error_log configuration option is a string and is the filename of where to log errors. It can contain either a relative path or an absolute path as part of the filename. If "syslog" is used it is logged to to the system logger (syslog on *nix and the Event Log on Windows).
Set error logging with ini_set()
Although this is not ideal you can use the ini_set() function to set the above configuration options to log errors. It is not an ideal solution because it will not log any errors if the script has parse errors and cannot be run at all. For example:
ini_set("log_errors", 1); ini_set("error_log", "/path/to/php-error.log");
Set error logging in .htaccess and virtualhost directives
This is a better option because it will log the errors even if there's a parsing error in your script.
php_value log_errors 1 php_value error_log /path/to/php-error.log
Example error log data
A couple of example lines from an error log are as follows:
[13-May-2009 21:54:04] PHP Notice: Undefined variable: x in /common/websites/test/error-log.php on line 6 [13-May-2009 21:54:09] PHP Parse error: syntax error, unexpected '}' in /common/websites/test/error-log.php on line 5
Error log must be writable by the web server
The error log will be written to as the user the web server runs as so it must be writeable by that user. In most cases you'll probably need to make it world-writeable to be able to do this. On a *nix server this is 0666 file permissions.
It's easy to set up a PHP error log using the log_errors and error_log configuration options. This can be set up on a website by website basis and you could potential combine it with a daily process to email errors to the webmaster (if there are any) on a daily basis. I'll cover doing this in a later post.