Skip to content Skip to navigation

Direct PHP errors to a log file of your choosing

Posted by: 
Thanks to John Bickar for the code below.

Developers working with sites on don’t get direct access to Apache’s error log. To troubleshoot your PHP applications, you can create a local php.ini file with the lines below. This will redirect PHP errors to a file of your choosing. Make sure to remove these lines and the file containing the errors once you are done troubleshooting to avoid disclosing the internal workings of your application.

error_log = errors.txt
log_errors = On
error_reporting = E_ALL
display_errors = Off

Put that in the root of your web application directory; it will then write errors to errors.txt. Note that only PHP errors will be saved. Errors at the Apache level will still go to the Apache error log.

If you'll need the error log for a while, you should protect it using WebAuth so that only you can see it. The following directives in an .htaccess file will accomplish that:

<Files 'errors.txt'>
AuthType WebAuth
require user your-sunet-id

Note: Error logs can grow quite large over time, take up your entire storage quota, and prevent the site from functioning correctly. Make sure to stop saving errors to a file once you are done troubleshooting.