How to log PHP errors and warnings into a file
This class collects all the errors reported from a PHP script, classifies them as either general errors or fatal errors. After classification, it passes the error to the respective method which then calls the log function that writes them out.
Let’s define some constant variables here.
/** * if your error class file resides in a folder called classes and the index file is in the base folder use: * * define('ERR_HANDLER_PATH', substr(dirname(__file__), 0, strpos(dirname(__file__), 'classes') - 1) . '/'); // DO NOT change!! * * after using the above line comment or remove the fisrt line down here **/ define('ERR_HANDLER_PATH', dirname(__file__).'/'); // DO NOT change!! define('ERR_HANDLER_LOG_FOLDER', 'logs'); // Name of logs folder.. Create if it does not exist. define('ERR_HANDLER_ENABLED', 1); // Enable custom error handler? define('ERR_HANDLER_DISPLAY', 1); // Display a message on screen? define('ERR_APPEND_RAND_STRING', 0); // Adds random string to file name for security. Prevents someone attempting browser access. define('MASK_FILE_PATH', 0); // Hide file path if error occurs.. define('FILE_ERR_LOG_FILE', 'errors.log'); // File name of error log define('FILE_FATAL_ERR_LOG_FILE', 'fatal_errors.log'); // File name of fatal error log
We create our class which has four functions and each function calls the log function when invoked:
- generalErr — This function is invoked when a general error occurs.
- mailErr — This function is invoked when an email error occurs.(if you have an email sending script)
- fataErr — This function is invoked when a fatal error occurs and no further execution can happen.
- log — calls the write function which then writes the error into a file.
class Errs public function generalErr($error) Errs::log($error, FILE_ERR_LOG_FILE); > public function mailErr($error) Errs::log($error, FILE_ERR_LOG_FILE); > public function fatalErr($error) Errs::log($error, FILE_FATAL_ERR_LOG_FILE); > public function log($error, $file) if (is_dir(ERR_HANDLER_PATH . ERR_HANDLER_LOG_FOLDER)) write(ERR_HANDLER_PATH . ERR_HANDLER_LOG_FOLDER . '/' . Errs::raStr() . $file, trim($error) . linending() . '***** - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *****' . linending()); > > public function raStr() return (ERR_APPEND_RAND_STRING ? substr(md5(uniqid(rand(),1)), 3, 30) . '-' : ''); > > // Initiate the class.. $DDEH = new Errs();
Now we check to see if the error handler is enabled. If it is enabled, we turn of displaying all the errors to the user.
if (ERR_HANDLER_ENABLED) // Switch off display errors @ini_set('display_errors', 0); // Set error reporting level.. error_reporting(E_ALL); >
On the Error reporting levels, we’re going to be using E_ALL. PHP provides lots of them which you can look at here PHP Error Levels.
Here’s just a few that I think are useful together with their error codes.
Error | Error Code | Description |
---|---|---|
E_ERROR | 1 | Fatal runtime error that can be recovered, The execution of the script is stopped immediately |
E_WARNING | 2 | A runtime warning, not fatal and most errors fall here, execution is not stopped |
E_NOTICE | 8 | Runtime notice indicating something that could possibly be an error was encountered when running a script normally |
E_COMPILE_ERROR | 64 | Fatal error that occurs while the script was being compiled generated by the Zend Scripting engine |
E_USER_ERROR | 256 | A fatal user-generated error, generated by the PHP code using the function trigger_error() rather than the PHP engine |
E_USER_WARNING | 512 | NON FATAL USER GENERATED warning message, generated by the PHP code using the function trigger_error() rather than the PHP engine |
E_STRICT | 2048 | Not strictly an error but it’s triggered whenever PHP encounters code that could lead to problems or forward incompatibilities |
E_ALL | 32767 | All errors and warnings except from level E_STRICT |
TIP: Passing a value of (-1) to the error_reporting() function will show every possible error when new levels and constants are added in future PHP versions.
Now, we create a function that inserts a newline depending on the OS architecture.
function linending() $newline = "\r\n"; if (isset($_SERVER["HTTP_USER_AGENT"]) && strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), 'win')) $newline = "\r\n"; > else if (isset($_SERVER["HTTP_USER_AGENT"]) && strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), 'mac')) $newline = "\r"; > else $newline = "\n"; > return (defined('PHP_EOL') ? PHP_EOL : $newline); >
This function will be called whenever a fatal error occurs.
function sysFatalErr() global $DDEH; $error = error_get_last(); if (isset($error['type'])) if ((defined('E_ERROR') && $error['type'] == E_ERROR) || $error['type'] == 4) $string = '[Error Code: ' . $error['type'] . '] ' . $error['message'] . linending(); $string .= '[Date/Time: ' . date('j F Y @ H:iA') . ']' . linending(); $string .= '[Fatal error on line ' . $error['line'] . ' in file ' . $error['file'] . ']'; if (ERR_HANDLER_DISPLAY) echo 'A fatal error has occurred. For more details please view "' . ERR_HANDLER_LOG_FOLDER . '/' . FILE_FATAL_ERR_LOG_FILE . '".'; > $DDEH->fatalErr($string); > > >
This other function is called when general errors occur, and can further classify them accordingly. You can add more in the switch block.
function sysErrorhandler($errno, $errstr, $errfile, $errline) global $DDEH; if (!(error_reporting() & $errno)) return; > if (!method_exists($DDEH,'generalErr') || !method_exists($DDEH,'fatalErr')) return; > switch ($errno) case E_USER_ERROR: $string = '[Error Code: ' . $errno . '] ' . $errstr . linending(); $string .= '[Date/Time: ' . date('j F Y @ H:iA') . ']' . linending(); $string .= '[Error on line ' . $errline . ' in file ' . $errfile . ']'; if (ERR_HANDLER_DISPLAY) echo 'A fatal error has occurred. For more details please view "' . ERR_HANDLER_LOG_FOLDER . '/' . FILE_FATAL_ERR_LOG_FILE . '".'; > $DDEH->fatalErr($string); exit; break; case E_USER_WARNING: $string = '[Error Code: ' . $errno . '] ' . $errstr; $string .= '[Date/Time: ' . date('j F Y @ H:iA') . ']' . linending(); $string .= '[Error on line ' . $errline . ' in file ' . $errfile . ']'; if (ERR_HANDLER_DISPLAY) echo 'An error has occurred. For more details please view "' . ERR_HANDLER_LOG_FOLDER . '/' . FILE_ERR_LOG_FILE . '".'; > $DDEH->generalErr($string); break; case E_USER_NOTICE: $string = '[Error Code: ' . $errno . '] ' . $errstr . linending(); $string .= '[Date/Time: ' . date('j F Y @ H:iA') . ']' . linending(); $string .= '[Error on line ' . $errline . ' in file ' . $errfile . ']'; if (ERR_HANDLER_DISPLAY) echo 'An error has occurred. For more details please view "' . ERR_HANDLER_LOG_FOLDER . '/' . FILE_ERR_LOG_FILE . '".'; > $DDEH->generalErr($string); break; default: $string = '[Error Code: ' . $errno . '] ' . $errstr . linending(); $string .= '[Date/Time: ' . date('j F Y @ H:iA') . ']' . linending(); $string .= '[Error on line ' . $errline . ' in file ' . $errfile . ']'; if (ERR_HANDLER_DISPLAY) echo 'An error has occurred. For more details please view "' . ERR_HANDLER_LOG_FOLDER . '/' . FILE_ERR_LOG_FILE . '".'; > $DDEH->generalErr($string); break; > return true; >
Finally, we write out our errors into a file. Remember to have the logs folder created.
function write($file, $data) file_put_contents($file, $data, FILE_APPEND); >
You can get the whole code on the GitHub repo Here
In need of a developer for a project? Hit me Up DentriceDev Solutions
How to Log Errors and Warnings in a File with PHP
PHP allows logging errors and warnings into a file with the help of a php file and modifying the configuration the php.ini file configuration.
Below, we will consider two methods to achieve that.
Using the error_log() Function
This function is used for sending error messages to a particular file. The first argument of the function is the error message that is going to be sent. The second argument sets the place for logging the error message (3 to save into the file, 1 to send an email). The aim of the third argument is to indicate the file path of the error logging file.
Now, let’s check out an example of using error_log() :
// php code for logging error into a given file // error message to be logged $error_message = "It is an error message!"; // path of the log file where errors need to be logged $log_file = "./my-errors.log"; // logging error message to given log file error_log($error_message, 3, $log_file); ?>
Using the ini_set() Function
With the help of the ini_set() function, you can upgrade the configuration of your php.ini file.
This function includes several handy commands:
- The ini_set(«log_errors», TRUE) command is added to the php script for enabling error logging in PHP.
- The ini_set(«error_log», $log_file) command is added to the script for setting the error logging file.
- The error_log($error_message) command is applied for logging an error message to a particular file.
Here is an example of this approach:
// PHP code to log error into a given file // error message to be logged $error_message = "This is an error message!"; // path of the log file where errors need to be logged $log_file = "./my-errors.log"; // setting error logging to be active ini_set("log_errors", true); // setting the logging file in php.ini ini_set('error_log', $log_file); // logging the error error_log($error_message); ?>
Before using this approach, note that it is not highly reliable on the contrary to the first approach. So, it is more recommended to use the 1st method as it allows you to choose different files at the same time without changing the php.ini file configuration.