Showing posts with label PHP log_errors. Show all posts
Showing posts with label PHP log_errors. Show all posts

Wednesday, 12 September 2018

Logging errors with 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.

php.ini

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.

Apache configuration

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.

.htaccess

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.

PHP script

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.
  1. PHP script
  2. .htaccess
  3. Apache configuration
  4. php.ini
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

Related posts:

Tuesday, 11 September 2018

Log PHP errors with log_errors and error_log

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.

log_errors

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.

error_log

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.

Conclusion

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.

Related posts: