thinkphp解决出错时输出的sql语句或参数不完整的问题

 异常时会调用  $e->getTraceAsString();  而这个函数会把参数长度超过15个字符的截断掉,变成 “…”

if (Z_STRLEN_PP(arg) > 15) {
    TRACE_APPEND_STRL(Z_STRVAL_PP(arg), 15); //这里会截取前15个字符
    TRACE_APPEND_STR("...', ");
    l_added = 15 + 6 + 1; /* +1 because of while (--l_added) */
 }

网上找 了个方法可以解决这个问题,先把 getExceptionTraceAsString()放入function.php里,然后打开Think.class.php到226左右,
把$error[‘trace’]     =   $e->getTraceAsString(); 换成 $error[‘trace’]     =   getExceptionTraceAsString($e);

Think.class.php 代码如下:

static public function appException($e) {
    $error = array();
    $error['message']   =   $e->getMessage();
    $trace              =   $e->getTrace();
    if('E'==$trace[0]['function']) {
        $error['file']  =   $trace[0]['file'];
        $error['line']  =   $trace[0]['line'];
    }else{
        $error['file']  =   $e->getFile();
        $error['line']  =   $e->getLine();
    }
    $error['trace']     =   getExceptionTraceAsString($e);   //修改了此行
    Log::record($error['message'],Log::ERR);
    // 发送404信息
    header('HTTP/1.1 404 Not Found');
    header('Status:404 Not Found');
    self::halt($error);
}

function.php代码如下:

function getExceptionTraceAsString($exception) {
    $rtn = "";
    $count = 0;
    foreach ($exception->getTrace() as $frame) {
        empty($frame['file']) && $frame['file'] = "[internal function]";
        empty($frame['class']) || $frame['class'] = $frame['class']."->";
        $args = "";
        if (isset($frame['args'])) {
            $args = array();
            foreach ($frame['args'] as $arg) {
                if (is_string($arg)) {
                    $args[] = "'" . $arg . "'";
                } elseif (is_array($arg)) {
                    $args[] = "Array";
                } elseif (is_null($arg)) {
                    $args[] = 'NULL';
                } elseif (is_bool($arg)) {
                    $args[] = ($arg) ? "true" : "false";
                } elseif (is_object($arg)) {
                    $args[] = get_class($arg);
                } elseif (is_resource($arg)) {
                    $args[] = get_resource_type($arg);
                } else {
                    $args[] = $arg;
                }
            }
            $args = join(", ", $args);
        }
        $rtn .= sprintf( "#%s %s(%s): %s%s(%s)\n",
            $count,
            $frame['file'],
            $frame['line'],
            $frame['class'],
            $frame['function'],
            $args );
        $count++;
    }
    return $rtn;
}

文章转载自:http://www.thinkphp.cn/topic/34335.html

暂无评论

发送评论 编辑评论


				
上一篇
下一篇