≡ Menu

How to log all PHP errors to a file?

The question – I want to log all PHP errors to a log file. Is there a PHP directive or statement that can achieve this?

PHP has a way to log all errors to a log file using the error_log directive. This directive can be set in your php.ini file:

First locate your php.ini:

[root@server ~]# php -i | grep php.ini
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini
[root@server ~]#

Now you know the location of php.ini (/etc/php.ini). Run the following command:

[root@server ~]# grep -i error_log /etc/php.ini
;error_log = php_errors.log
;error_log = syslog
[root@server ~]#

As you can see, both statements are commented out. So no logging is being done.

To enable logging, you must change your php.ini and add the following statement:

error_log = /var/log/php_errors.log

Make sure that you have removed the the semi-colon from the beginning of the line to uncomment it.

Now you must restart Apache:

service httpd restart

All PHP errors will not be logged to the /var/log/php_errors.log file.

To view the file, you can use the tail command, or open the log file in your favorite editor like vi.

Comments on this entry are closed.