Logging errors with PHPPosted in PHP -
PHP has several settings for logging errors; in this post I will look at the log_errors and error_log settings which control whether errors should be logged and where to.
How to log errors
The error_reporting configuration option controls what level of errors are reported, and also what level of errors will be logged to the error log. If E_ALL is set then all errors (including notices) will be logged. If it's set to E_ALL ^ E_NOTICE then everything but notices will be logged.
The two variables needed to log errors are:
- log_errors - this needs to be set to 1 or "On"
- error_log - the name of the file to write the errors to; it must be writeable by the webserver
These configuration options can be set in the php.ini, Apache configuration, an .htaccess file or in a PHP script. Note that if the PHP script could not be parsed then nothing will be logged, so it's generally best to configure it in one of the others.
Open the php.ini file and locate the log_errors and error_log options setting them like so:
log_errors = On error_log = /path/to/error.log
Subsitute /path/to/error.log to where you want the error log to be. Once the file is saved gracefully reload or restart Apache and error logging will begin.
To make the setting global for all websites use php.ini; to instead make it work for a single virtual host use the Apache configuration, adding the following to the appropriate <virtualhost> block. If you don't know what this means then you are best to use a .htaccess file as shown in the next section.
<virtualhost *:80> ... php_value log_errors on php_value error_log /path/to/error.log ... </virtualhost>
Subsitute /path/to/error.log to where you want the error log to be. Once the configuration changes have been made gracefully reload or restart Apache and error logging will begin.
Create a new .htaccess file in the website's root directory or modify the existing one if you already have one by adding the following to it:
php_value log_errors on php_value error_log /path/to/error.log
Apache does not need to be restarted for the changes to take effect.
And finally it is possible to configure error logging in the PHP script itself, although as I have already mentioned it is better to do it one of the other ways because parse errors will not be logged. If you do decided to set up error logging in the script add this to the start of the script or one of the base include files:
ini_set("log_errors", "On"); ini_set("error_log", "/path/to/error.log");
Order of precedence
It is possible to set up error logging using any or all of the methods above. If error logging is configured in more than one location errors are still only logged once, using the defined rules in order of the following priority.
- PHP script
- Apache configuration
For example, if the error logging settings are defined in both the PHP script and the .htaccess file then the settings in the script override those in the .htaccess file.
Note that if the PHP script could not be parsed then it will fall back to the next precedence, if set. For example if an error log was specified in the php.ini file and in the PHP script but the PHP script could not be parsed, then the error would be logged to where specified in the php.ini