public static function handler($exception) { if ($exception instanceof Exception) { $debugging = defined('IS_DEBUGGING') ? IS_DEBUGGING : false; $production = defined('IS_PRODUCTION') ? IS_PRODUCTION : false; if (true == $debugging) { if (true == $production) { LJL_Log::write(API_String::clean($exception), LJL_Log::TYPE_EXCEPTION); } else { echo LJL_Request::resolveType() == LJL_Request::CLI ? API_String::clean($exception) : $exception; } } else { header('location: ' . SYSTEM_HOMEPAGE); } } }
public static function handler($level, $errorMsg, $file, $line, $context = null) { if ('.tpl.php' == substr($file, -8)) { return; } $str = new LJL_Exception($errorMsg, $level, $file, $line); $debugging = IS_DEBUGGING; $production = IS_PRODUCTION; if ($debugging) { $content = "<br />\n<h2>Error Info:</h2>\n" . '<b>MESSAGE:</b> ' . $errorMsg . "<br />\n" . '<b>TYPE:</b> ' . (isset(self::$levels[$level]) ? self::$levels[$level] : $level) . "<br />\n" . '<b>FILE:</b> ' . $file . "<br />\n" . '<b>LINE:</b> ' . $line . "<br />\n" . $str; if ($production) { LJL_Log::write(API_String::clean($content), LJL_Log::TYPE_ERROR); } else { echo LJL_Request::resolveType() == LJL_Request::CLI ? API_String::clean($content) : $content; } } }
/** * 查询 * * @param string $sql * @return PDOStatement */ public function query($sql = '') { static $reconnectNum = 0; #添加日志功能,重置时间 if (IS_DEBUGGING) { LJL_Log::resetTime(); } $this->chooseDbConn($sql); $sql .= $this->sqlComment; $query = $this->db->query($sql); if (empty($query)) { $error = $this->errorInfo(); if ($reconnectNum < 5 && $error[0] == 'HY000') { $this->db = null; $reconnectNum++; if ($reconnectNum > 1) { sleep($reconnectNum); } return $this->query($sql); } trigger_error($error[2], E_USER_WARNING); } $reconnectNum = 0; #添加日志功能 if (IS_DEBUGGING && isset($_SERVER["REQUEST_URI"])) { $nowTime = date("H:i:s"); $nowUrl = str_replace("_check_mysql_query=", "", $_SERVER["REQUEST_URI"]); $sql = str_replace("\n", "", $sql); $sql = preg_replace("#\\s{2,}#", " ", $sql); $logContent = "{$nowUrl} [{$nowTime}][" . $this->servers['slave']['host'] . " - " . $this->servers['slave']['database'] . "] SQL:" . $sql . " \n"; LJL_Log::checkUriAndWrite(array('message' => $logContent, 'paramName' => '_check_mysql_query', 'recTime' => true)); } return $query; }
/** * 重设时间,为了统计某一个执行的时间用 */ public static function resetTime() { self::$timer = microtime(true); }
/** * 利用curl的形式获得页面请求 请用这个函数取代file_get_contents */ public static function curlPage($paramArr) { if (is_array($paramArr)) { $options = array('url' => false, 'timeout' => 2, 'recErrLog' => 0, 'reConnect' => 0, 'keepAlive' => 0); $options = array_merge($options, $paramArr); extract($options); } $timeout = (int) $timeout; if (0 == $timeout || empty($url)) { return false; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) { curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); #避免首先解析ipv6 } if ($keepAlive) { $rch = curl_copy_handle($ch); curl_setopt($rch, CURLOPT_FORBID_REUSE, false); curl_setopt($rch, CURLOPT_HTTPHEADER, array('Connection: Keep-Alive', 'Keep-Alive: 3')); $data = curl_exec($rch); } else { $data = curl_exec($ch); } #记录错误日志 if ($recErrLog || $reConnect) { $errNo = curl_errno($ch); if ($reConnect && (28 == $errNo || 7 == $errNo || 6 == $errNo)) { #超时重连 6:name lookup timed out $errMsg = curl_error($ch); $data = self::curlPage(array('url' => $url, 'timeout' => 1, 'recErrLog' => 1, 'reConnect' => 0)); #这次不需要重连 LJL_Log::write("[api_curl_toreconn][{$url}] [{$errNo}]{$errMsg}", LJL_Log::TYPE_ERROR); } elseif ($errNo && $recErrLog) { #记录错误 $errMsg = curl_error($ch); LJL_Log::write("[api_curl][{$url}] [{$errNo}]" . $errMsg, LJL_Log::TYPE_ERROR); } } if (!$keepAlive) { curl_close($ch); } return $data; }