Logging errors to file in PHP

PHP offers simple but effective solution to log all errors to a log file. On all production web server it is a must that you turn off displaying error to end users via a web browser. Remember PHP gives out lots of information about path, database schema and all other sort of sensitive information. You are strongly advised to use error logging in place of error displaying on production web sites. The idea is quite simple -only developer should see php error log.

The way I choose to do it is by having a common config.php file containing this code and included in the first line of every page.

// Display no errors in runtime pages
ini_set("display_errors" , "0"); 

// Log errors to file
ini_set("log_errors" , "1");

// Error log file path and name
ini_set("error_log" , "logs/Errors.log.txt");

Good luck!!!

Abhishek Gupta
Follow me
Latest posts by Abhishek Gupta (see all)

One Reply to “Logging errors to file in PHP”

Leave a Reply