How can I get the full string of PHP’s getTraceAsString()?

前端 未结 7 1394
南笙
南笙 2020-12-08 10:32

I\'m using getTraceAsString() to get a stack trace but the string is being truncated for some reason.

Example, an exception is thrown and I log the stri

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 11:21

    That solution is good but in my case it threw an error because my trace has internal functions in it. I added a few lines of code to check for that so the trace functions still works.

    function getExceptionTraceAsString($exception) {
        $rtn = "";
        $count = 0;
        foreach ($exception->getTrace() as $frame) {
    
    
            $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);
            }
            $current_file = "[internal function]";
            if(isset($frame['file']))
            {
                $current_file = $frame['file'];
            }
            $current_line = "";
            if(isset($frame['line']))
            {
                $current_line = $frame['line'];
            }
            $rtn .= sprintf( "#%s %s(%s): %s(%s)\n",
                $count,
                $current_file,
                $current_line,
                $frame['function'],
                $args );
            $count++;
        }
        return $rtn;
    }
    

提交回复
热议问题