1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?php
/*
CREATE TABLE IF NOT EXISTS `_Exceptions` ( `IdException` int(11) NOT NULL auto_increment, `Instant` datetime NOT NULL, `Message` varchar(200) NOT NULL, `Line` int(11) NOT NULL, `File` varchar(200) NOT NULL, `SQL` text, `Trace` text NOT NULL, `Details` text NOT NULL, PRIMARY KEY (`IdException`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
*/
class myException extends Exception { private $sql; private $instant; public function __construct($msg, $requete = NULL) { parent::__construct($msg); // don't forget to call parent constructor $this->sql = $requete; $this->instant = time(); } public function getError() { $return = 'An exception has been generated :<br />'; $return .= '<b>Date</b> : '.date('d/m/Y', $this->instant).' at '.date('H:i:s', $this->instant).'<br />'; $return .= '<b>Message</b> : '.$this->message.'<br />'; $return .= '<b>Line</b> : '.$this->line.'<br />'; $return .= '<b>File</b> : '.$this->file.'<br />'; if(!is_null($this->sql)) $return .= '<b>SQL</b> : '.$this->sql.'<br />'; $return .= '<b>Trace</b> : <br />'.nl2br($this->getTraceAsString()).'<br />'; $return .= '<b>Details</b> : <br />'.nl2br(str_replace(' ', '.', print_r($this->getTrace(), true))).'<br />'; return $return; } public function sendError() { $shipperName = 'EXCEPTION LOG'; $shipperMail = 'exception@email.com'; $headers ='From: "'.$shipperName.'"<'.$shipperMail.'>'."\n"; $headers .='Content-Type: text/html; charset="iso-8859-1"'."\n"; $recipient = 'my@email.com'; $subject = 'My PHP application : an exception has been generated'; $message ='<html><body>'.$this->getError().'</body></html>'; mail($recipient, $subject, $message, $headers); } public function saveError() { $timestamp = quote_smart(date('Y-m-d H:i:s', $this->instant)); $message = quote_smart($this->message); $line = quote_smart($this->line); $file = quote_smart($this->file); $trace = quote_smart($this->getTraceAsString()); $details = quote_smart(print_r($this->getTrace(), true)); if(is_null($this->sql)) $sql = 'NULL'; else $sql = "'".quote_smart($this->sql)."'"; $request = "INSERT INTO _Exceptions VALUES( NULL, '{$timestamp}', '{$message}', {$line}, '{$file}', {$sql}, '{$trace}', '{$details}'); "; if(!mysql_query($request)) throw new Exception('Insertion error for an exception : '.$request); // Exception, not myException, else recursivity is possible... } }
?>
|