背景
- 利用Sentry1.11自动捕获ThinkPhp6的异常。
问题
- 以前使用ThinkPhp5.1时在public中的index.php入口文件写入sentry的自动捕获异常代码即可自动捕获异常,代码如下:
$sentry = require __DIR__ . '/../config/sentry.php';
if(!empty($sentry['core_key'])) {
$client = new \Raven_Client($sentry['core_key']);
$error_handler = new \Raven_ErrorHandler($client);
$error_handler->registerExceptionHandler();
$error_handler->registerErrorHandler();
$error_handler->registerShutdownFunction();
}
- 目前使用ThinkPhp6写入对应代码无法自动捕获,具体原因还未找到,欢迎大家评论发言。
解决方案
- 看TP6.0框架手册后发现异常类默认托管在app/provider.php中,如图:
- 在ExceptionHandle中,手动添加Sentry报错,如下代码:
<?php
namespace app;
use app\common\utils\SentryLog;
use think\console\Output;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Response;
use Throwable;
class ExceptionHandle extends Handle
{
protected $ignoreReport = [
HttpException::class,
HttpResponseException::class,
ModelNotFoundException::class,
DataNotFoundException::class,
ValidateException::class,
];
public function report(Throwable $exception): void
{
parent::report($exception);
}
public function render($request, Throwable $e): Response
{
SentryLog::error(config('sentry.core_key'), self::_getErrorMsgData($e, 'http'));
return parent::render($request, $e);
}
public function renderForConsole(Output $output, Throwable $e): void
{
SentryLog::error(config('sentry.core_key'), self::_getErrorMsgData($e, 'console'));
parent::renderForConsole($output, $e);
}
private static function _getErrorMsgData(Throwable $e, $render)
{
$exception = [
'error' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'render' => $render,
'code' => $e->getCode(),
'time' => date('YmdHis')
];
return json_encode($exception, 256|JSON_UNESCAPED_SLASHES);
}
}
- 以上代码中render方法主要捕获Http异常,renderForConsole主要捕捉Console错误,为全面捕捉,固两个方法都进行重写。
- 以上代码中SentryLog方法为自己进行封装,代码如下:
<?php
declare (strict_types = 1);
namespace app\common\utils;
class SentryLog
{
public function __construct($key)
{
$this->client = new \Raven_Client($key);
}
public function send($message, $level, $extra = [], $tags = [])
{
$extra['_trace'] = debug_backtrace();
$this->client->captureMessage($message, [], [
'level' => $level,
'extra' => $extra,
'tags' => $tags
]);
}
public static function info($key, $message, $extras=[], $tags=[])
{
$static = new static($key);
$static->send($message, 'info', $extras, $tags);
}
public static function debug($key, $message, $extras=[], $tags=[])
{
$static = new static($key);
$static->send($message, 'debug', $extras, $tags);
}
public static function warning($key, $message, $extras=[], $tags=[])
{
$static = new static($key);
$static->send($message, 'warning', $extras, $tags);
}
public static function error($key, $message, $extras=[], $tags=[])
{
$static = new static($key);
$static->send($message, 'error', $extras, $tags);
}
}
最后
- 以上为自行进行处理的方案,若各位有好的处理方案欢迎大家评论留言,让我们一同进步。