说明:Laravel之bootstrap源码解析中聊异常处理
时提到过Sentry这个神器,并打算之后聊聊这款神器,本文主要就介绍这款Errors Tracking神器Sentry
,Sentry官网有一句话我的以为帅呆了:php
Stop hoping your users will report errors.laravel
Sentry是一款可用于Production环境的错误跟踪工具,可实时通知Production环境中用户因为一些不可预料行为(或者程序写的有问题)致使程序Crash或Exception,Sentry能够经过Integration如HipChat来发送通知,而且能够经过JIRA Integration来快速建立Issue,而后开发者能够根据这个Issue快速修复程序,并把这个已修复的Hotfix快速部署到生产环境,这样就快速开发快速修补。天下武功,惟快不破。
bootstrap
本文主要推荐下这款神器,并介绍下它的安装和配置,有兴趣的能够关注下这款神器。而且这款神器已经在RightCapital获得长时间应用了,结合HipChat和JIRA用起来很顺手,值得推荐。api
开发环境:Laravel5.3 + PHP7
浏览器
使用Sentry有两种方式:Sentry Cloud和Sentry Server。Sentry Cloud就是直接使用Sentry提供的服务,注册个帐号后而后进行设置就可使用了,这样Production Code就会把Exception这些敏感数据发送到Sentry Cloud,不过公司使用不建议这么作,毕竟这些Exceptions是有不少敏感数据,而这些数据是放在别人家的云服务器上,谁知道会发生什么呢;Sentry Server是Python写的,能够部署在本身的云服务器上如AWS或Aliyun,如我司是部署在AWS云上,Sentry官方推荐使用Docker Image来部署。固然,无论哪种方式,使用仍是同样的。就有点像Github/Gitlab、Bitbucket/Bitbucket Server。服务器
这里就介绍下Sentry Cloud如何使用,只有一个用户时,Sentry天天免费5000 events:app
首先是注册个帐号。这个去官网注册下就OK了。composer
安装Sentry包。Sentry提供针对几乎每种语言的平台Sentry Platform,这里介绍下如何在Laravel程序中集成Sentry。ide
Sentry for Laravel中介绍了如何集成进Laravel,主要就是安装下Sentry Laravel包:工具
// 生产环境也须要这个包,不须要加 --dev composer require sentry/sentry-laravel 'providers' => array( Sentry\SentryLaravel\SentryLaravelServiceProvider::class, ) 'aliases' => array( 'Sentry' => Sentry\SentryLaravel\SentryFacade::class, ) php artisan vendor:publish --provider="Sentry\SentryLaravel\SentryLaravelServiceProvider"
在本身的程序中安装好包后,而后在.env配置下SENTRY_DSN
。登陆进刚刚注册的帐号后,先建立个Project获得这个Project的SENTRY_DSN
(点击 New Project
):
而后点击左上角选择刚刚建立的Project如我的建立的Sentry/Development
,而后点击左侧栏的Client Keys
就行,把DSN值copy出来填入.env文件中(不是DSN Public值),Sentry_DSN结构是:https://{public_key}:{private_key}@sentry.io/{project_id}
:
// .env
SENTRY_DSN=Your_Sentry_DSN
同时在Add Integrations,点击左侧All Integrations选择HipChat后,而后选择左侧的HipChat按钮,选择Enable Integration
,这样就跳入了HipChat中Integration页面,赞成集成就行,若是没注册HipChat帐号就注册下就行,HipChat是Atlassian旗下的一款免费的聊天协做工具,电脑端手机端均可以安装,值得推荐。固然,Atlassian全家桶SourceTree(免费)、JIRA(免费/收费)、Bitbucket(免费/收费)、Confluence(免费/收费)、Bamboo(免费/收费)也都值得推荐。
Laravel中异常处理类\App\Exceptions\Handler主要包含两个方法report()
和sender()
,其中report()
就是主要用来向第三方service发送异常报告,这里选择向Sentry这个神器发送异常报告,并使用HipChat通知开发人员。这里在report()
写上代码:
/** * A list of the exception types that should not be reported. * * @var array */ protected $dontReport = [ // \Illuminate\Auth\AuthenticationException::class, // \Illuminate\Auth\Access\AuthorizationException::class, // \Symfony\Component\HttpKernel\Exception\HttpException::class, // \Illuminate\Database\Eloquent\ModelNotFoundException::class, // \Illuminate\Session\TokenMismatchException::class, // \Illuminate\Validation\ValidationException::class, ]; /** * Report or log an exception. * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. * * @param \Exception $exception * @return void */ public function report(Exception $exception) { // If the exception is instanceOf $dontReport, do not report it. if ($this->shouldntReport($exception)) { return; } // Send exception to Sentry if it is set. if (env('SENTRY_DSN')) { Sentry::captureException($exception); } else { // Log the exception if the Sentry is not set. parent::report($exception); } }
shouldntReport()
会读取$dontReport[ ]值,查找有哪些Exceptions是不须要Report的,在生产环境能够都注销掉,表示用户产生的全部异常都须要发送到Sentry中,并经过手机端HipChat告知开发者,而后使用Sentry::captureException()
捕获异常。固然有时因为业务需求,如根据不一样模块报异常level不同,须要定制下Sentry类,这里只是简单捕获异常,并默认为都是error level
。
OK,全部的工做就这么简单的完成了。
试一下,如在浏览器中输入一个不存在的路由如http://sentry.app:8888/sentry
,而后报NotFoundHttpException,查看Sentry有没有捕获到:
而后查看HipChat有没有收到通知:
这里每一次report就是一个event,Sentry对于我的使用是天天免费5000 events。
Sentry的Exception Stack内容很详细,很快就能定位bug在哪,并且还捕获了不少tags,如用户的device,browser,environment等等有用信息,这些信息均可以用来快速定位bug。经过Exception Stack也能发现Laravel的执行流程。
总结:本文主要介绍一款异常捕获神器Sentry,值得推荐,具体使用能够深挖Sentry官网文档和博客,这种提升生产率的神器必须深挖。