Exception handling&error log
![LARAVEL-errorhanding](https://laravel.gstatics.cn/wp-content/uploads/2017/11/LARAVEL-errorhanding.png)
brief introduction
App\Exceptions\Handler
Note: For the underlying principle and implementation of Laravel exception handling, refer to the college's tutorial for more information—— "In depth discussion of PHP error exception handling mechanism and corresponding implementation of the underlying Laravel framework"
to configure
Error Details
config/app.php
debug
.env
APP_DEBUG
Log storage
single
daily
syslog
errorlog
config/app.php
log
'log' => 'daily'
single : So the log information will be recorded in a single log file syslog : By system syslog Service processing log information errorlog : via PHP error_log Processor processing log information
Note: The underlying processing mechanism can refer to
Illuminate\Log\LogServiceProvider Implementation logic in.
'log_max_files' => 30
Log error level
app.php
log_level
'log_level' => env('APP_LOG_LEVEL', 'error'),
Note: Monolog supports the following error levels:
debug 、
info 、
notice 、
warning 、
error 、
critical 、
alert 、
emergency 。
Customize Monolog Configuration
configureMonologUsing
bootstrap/app.php
$app
$app->configureMonologUsing(function($monolog) { $monolog->pushHandler(...); }); return $app;
'log_channel' => env('APP_LOG_CHANNEL', 'my-app-name'),
Exception handler
App\Exceptions\Handler
report
render
Report method
report
report
/** *Report or record exceptions * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. * * @param \Exception $e * @return void * @translator laravelacademy.org */ public function report(Exception $e){ if ($e instanceof CustomException) { // } return parent::report($e); }
public function isValid($value) { try { // Validate the value... } catch (Exception $e) { report($e); return false; } }
/** *List of 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\Validation\ValidationException::class, ];
Render method
render
/** *Render exceptions to HTTP responses * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e){ if ($e instanceof CustomException) { return response()->view('errors.custom', [], 500); } return parent::render($request, $e);
}
Reportable&Renderable Exception
report
render
report
render
<? php namespace App\Exceptions; use Exception; class RenderException extends Exception { /** * Report the exception. * * @return void */ public function report() { // } /** * Render the exception into an HTTP response. * * @param \Illuminate\Http\Request * @return \Illuminate\Http\Response */ public function render($request) { return response(...); } }
HTTP exception
abort
abort(404);
abort
Abort (403, 'Unauthorized operation');
Custom HTTP error page
resources/views/errors/404.blade.php
abort
HttpException
$exception
{{ $exception->getMessage() }}
journal
storage/logs
Log
<? php namespace App\Http\Controllers; use App\User; use Illuminate\Support\Facades\Log; use App\Http\Controllers\Controller; class UserController extends Controller { /** *Display the properties of the specified user * * @param int $id * @return Response */ public function showProfile($id) { Log::info('Showing user profile for user: '.$ id); return view('user.profile', ['user' => User::findOrFail($id)]); } }
Log::emergency($error); Log::alert($error); Log::critical($error); Log::error($error); Log::warning($error); Log::notice($error); Log::info($error); Log::debug($error);
Log::info('User failed to login.', ['id' => $user->id]);
$monolog = Log::getMonolog();