Jump to content
Sign in to follow this  
lilmer

How to catch all PHP errors in CodeIgniter Framework

Recommended Posts

The problem is that I've already implemented procedures that has no try-and-catch exceptions.

Is there any procedure or functions that I can catch all PHP errors occurs in every page Globally?

 

Share this post


Link to post
Share on other sites

I've actually been using set_error_handler in a pre_system hook with CodeIgniter. If on development machine I just see the errors, however if on production the errors are logged to a file, then once every 15 minutes the file is emailed to me.

 

Don't be tempted to email yourself with each error as it happens. I learned the hard way that if PHP loops through an array where a notice is generated it will send you as many emails as notices. I got 4000 emails one day!

<?php

function my_error_handler( $e_number, $e_message, $e_file, $e_line, $e_vars )
{
	// Debugging allows dev to log errors like production
	$debug = FALSE;

	// The amount of E_NOTICE + E_STRICT before we die()
	$error_limit = 25;

	// The number of errors counted on this request
	static $error_count = 1;

	// Set the text of type of error displayed
	switch( $e_number )
	{
		case E_USER_ERROR:
			$error_type = 'E_USER_ERROR';
			break;

		case E_USER_WARNING:
			$error_type = 'E_USER_WARNING';
			break;

		case E_USER_NOTICE:
			$error_type = 'E_USER_NOTICE';
			break;
		
		case E_WARNING:
			$error_type = 'E_WARNING';
			break;

		case E_NOTICE:
			$error_type = 'E_NOTICE';
			break;

		case E_STRICT:
			$error_type = 'E_STRICT';
			break;

		default:
			$error_type = 'UNKNOWN ERROR TYPE';
			break;
	}

	// Output for development or testing environments (unless debug === TRUE)
	if( ENVIRONMENT != 'production' && $debug !== TRUE )
	{
		echo '<hr />PHP ' . $error_type . ' #' . $e_number . ' - Date/Time: ' . date('n/j/Y H:i:s') . PHP_EOL .
			'<br />File: <b>' . $e_file . '</b>' . PHP_EOL . 
			'<br />Line: <b>' . $e_line . '</b>' . PHP_EOL . 
			'<br /><b>' . $e_message . '</b><hr />' . PHP_EOL;
	}

	// Log file for production environment
	else
	{
		// MAIN MESSAGE
		$message = '#---' . PHP_EOL . 
			'PHP ' . $error_type . ' #' . $e_number . ' - Date/Time: ' . date('n/j/Y H:i:s') . PHP_EOL .
			'File: ' . $e_file . PHP_EOL .
			'Line: ' . $e_line . PHP_EOL .
			'Message: ' . $e_message . PHP_EOL;

		// POST VARS
		if( isset( $_POST ) && ! empty( $_POST ) )
		{
			$message .= 'POST vars:' . PHP_EOL;

			foreach( $_POST as $k => $v )
			{
				$message .= '\t' . $k . ' = ' . $v . PHP_EOL;
			}
		}

		// REQUEST HEADERS
		if( $request_headers = apache_request_headers() )
		{
			$message .= 'Request headers:' . PHP_EOL;

			foreach( $request_headers as $k => $v )
			{
				$message .= '\t' . $k . ' = ' . $v . PHP_EOL;
			}
		}

		// REQUEST URI
		$message .= 'Request URI: ' . $_SERVER['REQUEST_URI'] . PHP_EOL;

		// ERROR COUNT
		$message .= 'Error Count: ' . $error_count . PHP_EOL;

		$message .= '#--' . PHP_EOL;

		// Second param (3) says to store error in specified log file
		error_log($message, 3, FCPATH . '/application/logs/php_errors/php_errors.log' );

		// If not an E_NOTICE or E_STRICT, die()
		if( $e_number != E_NOTICE && $e_number < E_STRICT )
		{
			die( '<br /><br /><span style="color:red;">A system error occurred. We apologize for the inconvenience.</span>');
		}

		// If too many E_NOTICE or E_STRICT, die()
		$error_count++;
		if( $error_count > $error_limit )
		{
			die( '<br /><br /><span style="color:red;">A system error occurred. We apologize for the inconvenience.</span><br /><span style="font-size:50%;">E_NOTICE + E_STRICT > ' . $error_limit . '</span>');
		}
	}

	// Don't execute PHP internal error handler
	return TRUE;
}

function my_error_handling()
{
	set_error_handler('my_error_handler', E_ALL);
}
Edited by sKunKbad

Share this post


Link to post
Share on other sites

Don't be tempted to email yourself with each error as it happens. I learned the hard way that if PHP loops through an array where a notice is generated it will send you as many emails as notices. I got 4000 emails one day!

ignore_repeated_errors can help with that.

 

Another trick is to use Linux's logrotate: it can keep individual error files small by rotating them: error.log becomes error.log.1 becomes error.log.1.gz and so on. You can have it email you the error file at the same time.

Share this post


Link to post
Share on other sites

Thank you for your response, 

 

I set it using codeigniter hooks, using PHP set_error_handler, register_shutdown_function for fatal erros.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.