目錄
日志
composer create-project laravel/laravel log --prefer-dist "5.5.*"
# cd log
vim routes/web.php
<?php
Route::get('/', function () {
Log::info('This is some useful information.');
Log::warning('Something could be going wrong.');
Log::error('Something is really going wrong.');
return 'index';
});
echo "APP_LOG=daily" >> .env
- 測(cè)試
php artisan serve
curl localhost:8000 # 返回"index"
cat $(find storage/logs -name "[0-9a-zA-Z]*" | tail -n1)
[2018-04-27 09:29:23] local.INFO: This is some useful information.
[2018-04-27 09:29:23] local.WARNING: Something could be going wrong.
[2018-04-27 09:29:23] local.ERROR: Something is really going wrong.
#Linux
sed -i "s/APP_LOG_LEVEL=debug/APP_LOG_LEVEL=warning/g" .env
# MacOS
sed -i "" "s/APP_LOG_LEVEL=debug/APP_LOG_LEVEL=warning/g" .env
- 測(cè)試
# 更新.env配置后服務(wù)需要重啟生效
php artisan serve
rm storage/logs/*.log
curl localhost:8000 # 返回"index"
cat $(find storage/logs -name "[0-9a-zA-Z]*" | tail -n1)
[2018-04-27 09:36:31] local.WARNING: Something could be going wrong.
[2018-04-27 09:36:31] local.ERROR: Something is really going wrong.
錯(cuò)誤
composer create-project laravel/laravel bugsnag --prefer-dist "5.5.*"
注意
錯(cuò)誤處理在app/Exceptions/Handler.php文件中
cat app/Exceptions/Handler.php| tail -25
/**
* 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)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}
}
其中
在report函數(shù)中將錯(cuò)誤上報(bào)到第三方錯(cuò)誤統(tǒng)計(jì)平臺(tái)中
在render函數(shù)中處理異常渲染HTTP響應(yīng)
下面
我們將以第三方錯(cuò)誤統(tǒng)計(jì)平臺(tái)Bugsnag為例 來看一下錯(cuò)誤處理的效果
# cd bugsnag
composer require "bugsnag/bugsnag-laravel:^2.0"
vim config/app.php
return [
'providers' => [
/*
* Package Service Providers...
*/
Bugsnag\BugsnagLaravel\BugsnagServiceProvider::class,
],
];
echo "BUGSNAG_API_KEY=***" >> .env
這里的BUGSNAG_API_KEY是在Bugsnag注冊(cè)后得到的
vim app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
}
public function register()
{
$this->app->alias('bugsnag.logger', \Illuminate\Contracts\Logging\Log::class);
$this->app->alias('bugsnag.logger', \Psr\Log\LoggerInterface::class);
}
}
vim routes/web.php
<?php
use Bugsnag\BugsnagLaravel\Facades\Bugsnag;
Route::get('/', function () {
Bugsnag::notifyException(new RuntimeException("Test error"));
return 'index';
});
- 測(cè)試
php artisan serve
curl localhost:8000 # 返回"index"
此時(shí) 打開bugsnag管理后臺(tái)可以看到錯(cuò)誤報(bào)告如下
error-reporting-01.png