> # Sentry 建置 [TOC] # 介紹     Sentry 是一個監控錯誤日誌的平台。Sentry 會把 Exception 發生地點、程式跑過的地方、server 簡單的資訊、變數等等全部印出來,Sentry 可以自定義錯誤日誌的標籤與錯誤等級,以下將不介紹,有興趣的可以直接看官網使用。 ![](https://i.imgur.com/vUaBCoT.png) # sentry server docker 建置 如果沒有 docker 可以去官網查一下其他安裝方式,在 Reference 有附上官網連結。 ```bash # 拉所需 image $ docker pull redis; docker pull postgres; docker pull sentry; # 啟動 redis 與 postgres $ docker run -d --name sentry-redis --restart=always redis; $ docker run -d --name sentry-postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=sentry --restart=always postgres; # 產生 密鑰 $ docker run --rm sentry config generate-secret-key; jp7s921&xj+8u@29i#n=alr-mh6+d0^)0%!^tven*c+oa=q0&t # 初始化數據結構 $ docker run -it --rm -e SENTRY_SECRET_KEY='jp7s921&xj+8u@29i#n=alr-mh6+d0^)0%!^tven*c+oa=q0&t' --link sentry-postgres:postgres --link sentry-redis:redis sentry upgrade; # 啟動 sentry 所需要的三個容器 my-sentry:sentry的web服務 sentry-cron:sentry 的 cron sentry-worker:業務處理,警報等等 $ docker run -d -p 9000:9000 --name my-sentry -e SENTRY_SECRET_KEY='jp7s921&xj+8u@29i#n=alr-mh6+d0^)0%!^tven*c+oa=q0&t' --link sentry-redis:redis --link sentry-postgres:postgres sentry $ docker run -d --name sentry-cron -e SENTRY_SECRET_KEY='jp7s921&xj+8u@29i#n=alr-mh6+d0^)0%!^tven*c+oa=q0&t' --link sentry-postgres:postgres --link sentry-redis:redis sentry run cron $ docker run -d --name sentry-worker-1 -e SENTRY_SECRET_KEY='jp7s921&xj+8u@29i#n=alr-mh6+d0^)0%!^tven*c+oa=q0&t' --link sentry-postgres:postgres --link sentry-redis:redis sentry run worker ``` [Sentry](http://localhost:9000) - 因為我是建立在我遠端機器的 VM 上,以下圖示為遠端 domain ![](https://i.imgur.com/oM9KYc1.png) # php client 建置 sentry - PHP backend 建置流程 ![](https://i.imgur.com/ie6UKdl.png) - 根據 sentry 提示,安裝 laravel-sentry ![](https://i.imgur.com/c3nxCG5.png) ```bash= $ composer require sentry/sentry-laravel:1.1.0 ``` - 在 laravel 的 app/Exceptions/Handler.php 目錄 report function 加入... ```php= public function report(Exception $exception) { if (app()->bound('sentry') && $this->shouldReport($exception)){ app('sentry')->captureException($exception); } parent::report($exception); } ``` ```bash= $ php artisan vendor:publish --provider="Sentry\Laravel\ServiceProvider" ``` - 在 ```.env``` 檔案內加入變數,變數是Sentry根據專案產生出來的,以 sentry 提供的為準。 ```bash= SENTRY_LARAVEL_DSN=http://ccdc193426d6486894b368d9ab00faf4@10.206.60.22:8895/4 ``` # Testing - 測試,故意製造 exception ![](https://i.imgur.com/jjhf51Q.png) - Sentry 顯示錯誤 log ![](https://i.imgur.com/CRkTgkP.png) ![](https://i.imgur.com/vUaBCoT.png) # Sentry 變更時區 - 點選 user-settings ![](https://i.imgur.com/ULOuOhv.png) - 選擇你的 Timezone ,然後 save avatar ![](https://i.imgur.com/AoeSmTC.png) > 不要問我為什麼變更完 timezone 都提示儲存時區成功了,以後還要點 save avatar,如果沒有點的話在專案錯誤日誌那邊的時區還會是舊的。 ![](https://i.imgur.com/MKqjEuZ.png) # Reference - [建置 sentry](https://blog.csdn.net/super_rd/article/details/94721819) - [Sentry 官方](https://docs.sentry.io/enriching-error-data/context/?platform=php) > [name=zong xie][time=Tue, Dec 3, 2019 10:20 AM] ###### tags: `build`