custom error handler

Custom error handler – PHP

0
(0)

We will be craeting a custom error handler in PHP. As it name suggests, we will be displaying PHP errors in our own style.

Introduction

Error logs means to store a copy of an error that occurred in your website and saving it in a separate file. This helps you to keep track of all the errors in your website. By default PHP will display the error output on the screen. So if you are running a website on live server, it is always a good idea to keep track of any error, if happened.

For example, if a user perform some action that causes an logical error, so it causes a bug in your code. So you must know what and where that error occurred in your script. We can get the error code and the error code must be one of the following as mentioned in table. We can also get the error string which actually tells the error, for example, “undefined variable”, and file name which causes the error and also it’s line number.

Error levels

ValueConstantDescription
1E_ERRORFatal run-time error
2E_WARNINGRun-time warning
4E_PARSECompile-time parsing error
8E_NOTICEError in script/code
256E_USER_ERRORUser-generated error by calling trigger_error($error)
512E_USER_WARNINGUser-generated warning by calling trigger_error($error)
1024E_USER_NOTICECustom error
8192E_DEPRECATEDDeprecated code error
32767E_ALLAny type of error and warning

Following code will create a custom function and tells PHP to use this function instead of using built-in PHP error system. We will be storing logs for each day so the file name must be of today date. For this purpose, we are using PHP date($format) function to dynamically fetch today’s date. Then we are creating a variable to store error level, actual error, file name where error occurred and line number which causes the error.

We have echo the error so it will be visible in the browser, but can skip this line if you do not want to show the error to your users. We are using file_put_contents($filename, $content, $mode) function to save that error in file. You need to create a folder named logs where all error files will be saved. We will be using FILE_APPEND mode which will create a file if not exists, and append the data at the end of file if file of same name already exists.

PHP built-in error handler function

Finally we will call the set_error_handler($custom_function_name) function that tells the PHP to use custom function for handling errors. The parameter will be the name of function that we have just created, and it’s name must be in string. At the end, we have echo a variable which is not yet created, so it will trigger an error. We did this deliberately to generate an error.

<?php

// Create a custom error function
function on_error($error_no, $error, $filename, $linenumber)
{
	// get today date, saving logs for each day
	$today = date("Y-m-d");

	// Creating array for possible errors
	$error_levels = array(
		"1" => "Fatal error",
		"2" => "Warning",
		"8" => "Error",
		"1024" => "Custom error"
	);
	
	// Getting name of error by error level
	$str = $error_levels[$error_no] . ": ";

	// Display file name where error occurred
	$str .= $error . " in " . $filename;

	// Show line number which causes error
	$str .= " at " . $linenumber;

	// Moving to next line
	$str .= "\n";

	// Display error in browser
	// if you do not want to show errors to user,
	// then you can skip this line
	echo $str;

	// save the $str value in file
	file_put_contents("logs/" . $today . ".txt", $str, FILE_APPEND);
}

// Tells PHP to use custom function for errors
set_error_handler("on_error");

// Generating error deliberately
echo $a;

?>

If you are using Mac or Linux, you may also need to enable a folder to write permissions of the logs folder. That’s how you can create and use the custom error handler function in PHP and save errors in files as logs.

You can also enable all error reporting using default PHP functions:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

If you face any problems, feel free to ask in the comments section below.

How useful was this post

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post





Leave a Reply

Please disable your adblocker or whitelist this site!