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; } } }
/** * 检查url中的参数名,如果有该参数,就写日志 */ public static function checkUriAndWrite($paramArr) { $options = array('message' => '', 'paramName' => '', 'type' => self::TYPE_LOG, 'hasMark' => false, 'recTime' => false); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); if (isset($_SERVER["REQUEST_URI"]) && strpos($_SERVER["REQUEST_URI"], "?") !== false) { $addParam = substr($_SERVER["REQUEST_URI"], strpos($_SERVER["REQUEST_URI"], "?") + 1); parse_str($addParam, $addParamArr); if (isset($addParamArr[$paramName])) { if ($recTime) { #是否计算花费的时间 $message = " [" . round((microtime(true) - self::$timer) * 1000, 2) . " ms]" . $message; } LJL_Log::write($message, $type, $hasMark); } } }
/** * 利用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; }