/**
* 统一截获并处理错误
* @return bool
*/
public static function registErrorHandler() {
$e_types = array (
E_ERROR => 'PHP Fatal',
E_WARNING => 'PHP Warning',
E_PARSE => 'PHP Parse Error',
E_NOTICE => 'PHP Notice'
);
register_shutdown_function(function () use ($e_types) {
$error = error_get_last();
if ($error['type'] != E_NOTICE && !empty($error['message'])) {
$error['trace'] = self::getStackTrace();
self::error_handler($error);
}
});
set_error_handler(function ($type, $message, $file, $line) use ($e_types) {
if ($type != E_NOTICE && !empty($message)) {
$error = array (
'type' => $type,
'message' => $message,
'file' => $file,
'line' => $line,
'trace' => self::getStackTrace()
);
self::error_handler($error);
// 被截获的错误,重新输出到错误文件
error_log(($e_types[$type] ?: 'Unknown Problem') . ' : ' . $message . ' in ' . $file . ' on line ' . $line . "\n");
}
}, E_ALL);
}
/**
* @return IChainableException The previous exception.
*/
public function getPrevious();
public static function getStackTrace(Exception $e, $htmlFormat = false) {
$separator = $htmlFormat ? '<br/>' : "\n";
$stackTrace = '';
$currentException = $e;
while($currentException !== null) {
$stackTrace .= self::getExceptionSummary($currentException, $htmlFormat) . $separator;
if (method_exists($currentException, 'getPrevious') && $currentException->getPrevious() !== null) {
$stackTrace .= $separator . 'Caused by:' . $separator;
$currentException = $currentException->getPrevious();
} else {
$currentException = null;
}
}
return $stackTrace;
}
protected static function getExceptionSummary($e, $htmlFormat) {
$separator = $htmlFormat ? '<br/>' : "\n";
$spacer = $htmlFormat ? ' ' : "\t";
$emphasizer_begin = $htmlFormat ? '<b>' : '';
$emphasizer_end = $htmlFormat ? '</b>' : '';
$summary = $emphasizer_begin . get_class($e) . ': ' . $e->getMessage() . $emphasizer_end . $separator
. $spacer . self::getSafeFilePath($e->getFile()) . ' (line ' . $e->getLine() . ') ';
$fullTrace = $e->getTrace();
$fullTraceSize = count($fullTrace);
$i = 1;
foreach($fullTrace as $trace) {
// We skip the latest entry of the trace (the original include/require instruction)
if ($i == $fullTraceSize) {
break;
}
if (isset($trace['args'])) {
$flatArgs = implode(', ', array_map(array(__CLASS__, 'getBeautifiedArgument'), $trace['args']));
} else {
$flatArgs = 'void';
}
//if [file] and [line] are not set, it means it's the trace of the last function that has been
//started before the foreach(), so we skip the line separator (and spacer) here
if (!isset($trace['file']) && !isset($trace['line'])) {
$summary .= $trace['function'] . '(' . $flatArgs . ')';
} else {
$file = isset($trace['file']) ? self::getSafeFilePath($trace['file']) : '';
$line = isset($trace['line']) ? ' (line ' . $trace['line'] . ') ' : '';
$summary .= $separator . $spacer . $file . $line . $trace['function'] . '(' . $flatArgs . ')';
}
$i++;
}
return $summary;
}
错误抑制
ini_set('error_reporting',E_ALL);//开发阶段
ini_set('display_errors',1)//开发阶段
ini_set('error_reporting',E_ALL & ~ E_NOTICE & ~E_STRICT & ~E_WARNING);//开发阶段
ini_set('display_errors',0)//开发阶段 不显示错误
网友评论