美文网首页
PHP相关错误监控

PHP相关错误监控

作者: 我是你军哥 | 来源:发表于2017-10-26 00:18 被阅读14次
/**
 * 统一截获并处理错误
 * @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 ? '&nbsp;&nbsp;&nbsp;&nbsp;' : "\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)//开发阶段 不显示错误

相关文章

  • PHP相关错误监控

    错误抑制 开发阶段 生产阶段

  • 前端监控体系建设

    前端监控体系主要分为错误监控、性能监控、业务监控及安全监控三个方面。 一、错误监控 (一)监控范围 js语法错误、...

  • 日志-php-error错误日志查看

    日志-php-error错误日志查看 对于我们做php开发的人员,上了生产环境,一定要把相关debug,displ...

  • PHP的运行机制与原理

    PHP模块 内核、zend引擎、扩展层 php内核用来处理请求,文件流,错误处理等相关操作,zend引擎用以将源文...

  • 2019-06-17php版本变更5.6-7.0

    错误和异常处理相关的变更关于 在 PHP 7 中,很多致命错误以及可恢复的致命错误,都被转换为异常来处理了。 这些...

  • php运行原理

    PHP总共有三个模块:内核、Zend引擎、以及扩展层; PHP内核用来处理请求、文件流、错误处理等相关操作; Ze...

  • PHP底层分析

    编译流程 PHP总共有三个模块:内核、Zend引擎、以及扩展层;PHP内核用来处理请求、文件流、错误处理等相关操作...

  • Prometheus监控php-fpm

    php-fpm监控 php-fpm配置 nginx配置 php-fpm-exporter配置 下载地址: http...

  • Fundebug前端JavaScript插件更新至1.2.0

    摘要: Fundebug的前端JavaScript错误监控插件更新至1.2.0:支持监控WebSocket连接错误...

  • PHP异常、错误处理机制笔记

    本文介绍PHP的异常,错误以及如何屏蔽错误。参考:php异常、错误处理机制、PHP错误提示的关闭方法详解 首先要明...

网友评论

      本文标题:PHP相关错误监控

      本文链接:https://www.haomeiwen.com/subject/lwfhpxtx.html