Error Logging with .htaccess

7 October 2014

It's a good idea to not display any PHP error messages when you publish your web project to the world wide web. Therefore hiding those annoying messages a must, but how do you know why something went wrong? That's where error logging comes in! The easiest way to error log is to use the .htaccess file; this can be done as follows:

#disable error messages displaying
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off

# enable PHP error logging 
php_flag log_errors on
php_value error_reporting 32767
php_value error_log "logs/error.log.txt"

php_value error_reporting

This value defines the level of errors you wish to log:

  • 32767 Will log every error at runtime (fatals, warnings, parses, notices)
  • 8191 Will record every error apart from the runtime notices
  • 8 Will log compile time warnings and run time notices, warnings and parses.
  • 1 Will record runtime fatal errors only.

php_value error_log

This defines the error log file path (the path goes from the location of your .htaccess file). Also, please note that you may need to create the directories of where you wish to place your log file.

How the log is displayed

Great! So now when an error occurs there will not be a message to the user and the log will update with a new line, as follows:

[07-Oct-2014 15:20:58 UTC] PHP Fatal error:  Call to undefined function undefined_func() in C:\webserver\Apache24\htdocs\error logging htaccess\index.php on line 2