' . $message . '
PHP:' . $logTrace; self::writeErrorLog($messageSave); } if ($show) { self::showError('system', "
'; $errorMsg.= self::sqlClear($exception->getSql() , $exception->getDbConfig()); $errorMsg.= '
&#39;; } } else { $errorMsg = $exception->getMessage(); } $trace = $exception->getTrace(); krsort($trace); $trace[] = array( &#39;file&#39; => $exception->getFile() , &#39;line&#39; => $exception->getLine() , &#39;function&#39; => &#39;break&#39; ); $phpMsg = array(); foreach ($trace as $error) { if (!empty($error[&#39;function&#39;])) { $fun = &#39;&#39;; if (!empty($error[&#39;class&#39;])) { $fun.= $error[&#39;class&#39;] . $error[&#39;type&#39;]; } $fun.= $error[&#39;function&#39;] . &#39;(&#39;; if (!empty($error[&#39;args&#39;])) { $mark = &#39;&#39;; foreach ($error[&#39;args&#39;] as $arg) { $fun.= $mark; if (is_array($arg)) { $fun.= &#39;Array&#39;; } elseif (is_bool($arg)) { $fun.= $arg ? &#39;true&#39; : &#39;false&#39;; } elseif (is_int($arg)) { $fun.= (defined(&#39;SITE_DEBUG&#39;) && SITE_DEBUG) ? $arg : &#39;%d&#39;; } elseif (is_float($arg)) { $fun.= (defined(&#39;SITE_DEBUG&#39;) && SITE_DEBUG) ? $arg : &#39;%f&#39;; } else { $fun.= (defined(&#39;SITE_DEBUG&#39;) && SITE_DEBUG) ? &#39;&#39;&#39; . htmlspecialchars(substr(self::clear($arg), 0, 10)) . (strlen($arg) > 10 ? &#39; . . . &#39; : &#39;&#39;) . &#39;&#39;&#39; : &#39;%s&#39;; } $mark = &#39;, &#39;; } } $fun.= &#39;)&#39;; $error[&#39;function&#39;] = $fun; } if (!isset($error[&#39;line&#39;])) { continue; } $phpMsg[] = array( &#39;file&#39; => str_replace(array( SITE_PATH, &#39;&#39; ) , array( &#39;&#39;, &#39;/&#39; ) , $error[&#39;file&#39;]) , &#39;line&#39; => $error[&#39;line&#39;], &#39;function&#39; => $error[&#39;function&#39;] ); } self::showError($type, $errorMsg, $phpMsg); exit(); } /** * 记录错误日志 * * @static * @access public * @param string $message */ public static function writeErrorLog($message) { return false; // 暂时不写入 http://www.phprm.com $message = self::clear($message); $time = time(); $file = LOG_PATH . &#39;/&#39; . date(&#39;Y.m.d&#39;) . &#39;_errorlog.php&#39;; $hash = md5($message); $userId = 0; $ip = get_client_ip(); $user = &#39;User: userId=&#39; . intval($userId) . &#39;; IP=&#39; . $ip . &#39;; RIP:&#39; . $_SERVER[&#39;REMOTE_ADDR&#39;]; $uri = &#39;Request: &#39; . htmlspecialchars(self::clear($_SERVER[&#39;REQUEST_URI&#39;])); $message = " {$time} $message $hash $user $uri "; // 判断该$message是否在时间间隔$maxtime内已记录过,有,则不用再记录了 if (is_file($file)) { $fp = @fopen($file, &#39;rb&#39;); $lastlen = 50000; // 读取最后的 $lastlen 长度字节内容 $maxtime = 60 * 10; // 时间间隔:10分钟 $offset = filesize($file) - $lastlen; if ($offset > 0) { fseek($fp, $offset); } if ($data = fread($fp, $lastlen)) { $array = explode(" ", $data); if (is_array($array)) foreach ($array as $key => $val) { $row = explode(" ", $val); if ($row[0] != &#39;&#39;) { continue; } if ($row[3] == $hash && ($row[1] > $time - $maxtime)) { return; } } } } error_log($message, 3, $file); } /** * 清除文本部分字符 * * @param string $message */ public static function clear($message) { return str_replace(array( " ", " ", " " ) , " ", $message); } /** * sql语句字符清理 * * @static * @access public * @param string $message * @param string $dbConfig */ public static function sqlClear($message, $dbConfig) { $message = self::clear($message); if (!(defined(&#39;SITE_DEBUG&#39;) && SITE_DEBUG)) { $message = str_replace($dbConfig[&#39;database&#39;], &#39;***&#39;, $message); //$message = str_replace($dbConfig[&#39;prefix&#39;], &#39;***&#39;, $message); $message = str_replace(C(&#39;DB_PREFIX&#39;) , &#39;***&#39;, $message); } $message = htmlspecialchars($message); return $message; } /** * 显示错误 * * @static * @access public * @param string $type 错误类型 db,system * @param string $errorMsg * @param string $phpMsg */ public static function showError($type, $errorMsg, $phpMsg = &#39;&#39;) { global $_G; $errorMsg = str_replace(SITE_PATH, &#39;&#39;, $errorMsg); ob_end_clean(); $host = $_SERVER[&#39;HTTP_HOST&#39;]; $title = $type == &#39;db&#39; ? &#39;Database&#39; : &#39;System&#39;; echo <<$errorMsg
EOT; if (!empty($phpMsg)) { echo &#39;&#39;; echo &#39;
PHP Debug
&#39;; echo &#39;No. | File | Line | Code |
&#39; . $k . &#39; | &#39;; echo &#39;&#39; . $msg[&#39;file&#39;] . &#39; | &#39;; echo &#39;&#39; . $msg[&#39;line&#39;] . &#39; | &#39;; echo &#39;&#39; . $msg[&#39;function&#39;] . &#39; | &#39;; echo &#39;
|
效果图:PHP系统异常处理类程序