Jump to content

Error management


crabfinger

Recommended Posts

Hey guys, I'm working on a new content management system. Well... I've been working on it for a few years in a few different implementations, but i always end up losing the data, so now I'm starting again. And I didn't know where to start, so I started with the error manager. I need help from you guys with suggestions on what data would be helpful to have when reporting an error, how I should format the error message that is presented to the user, and how to make this code more efficient as well as more dynamic.

 

./classes/manageErrors.class.php

<?php
class manageErrors {
	function add_error($code) {
		if($error['message'] = $this->get_error($code)) {
			$error['code'] = $code;
			$error['time'] = time();
			$backtrace = debug_backtrace();
			foreach($backtrace as $key => $value) {
				if($key > 0) {
					$key = 'func' . str_pad($key,3,'0',STR_PAD_LEFT);
					$error[$key] = $value['function'] . '(' . implode(',',$value['args']) . ')';
					$error[$key] .= ' called on line ' . $value['line'];
					$error[$key] .= ' in file ' . $value['file'];
				}
			}
			$this->errors[] = $error;
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	function get_error($code=NULL) {
		$error_file = './conf/errors.conf';
		if(file_exists($error_file)) {
			$errors = file_get_contents($error_file);
			$errors = explode("\n",trim($errors));
			foreach($errors as $key => $value) {
				$error = explode(':',$value);
				$errors[$error['0']] = $error['1'];
			}
			if(is_numeric($code)) {
				if(isset($errors[$code])) {
					return $errors[$code];
				}
				else {
					return FALSE;
				}
			}
			else {
				return $errors;
			}
		}
	}
	function format($format='[%time%] (%code%) %message%',$date='h:i:s') {
		if(isset($this->errors)) {
			foreach($this->errors as $errori => $errora) {
				$string_temp = $format;
				foreach($errora as $key => $value) {
					if($key == 'time') {
						$value = date($date,$value);
					}
					$string_temp = str_replace('%' . $key . '%',$value,$string_temp);
				}
				$string .= $string_temp . "\r\n";
			}
			return 'The following errors have been logged and sent to a site administrator for analysis. We will fix this problem as soon as we can, please try again later.' . "\r\n" . $string;
		}
		else {
			return 'No errors occured during the execution of this script.';
		}
	}
}
$errors = new manageErrors();
?>

 

./conf/errors.conf

001:A function was called that requires a different type of variable than the one supplied.

 

Thanks in advance for all of your help.

Link to comment
https://forums.phpfreaks.com/topic/187240-error-management/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.