doushe8577
2017-09-29 05:42
浏览 119
已采纳

Laravel:记录错误,并显示一个很好的视图

I'm trying to log errors in Laravel but it throws the Chrome default 500 error page before even reaching the render function, how am I supposed to catch any errors?

I want to log all errors to the database and show a nice user friendly CUSTOM view but it how can I when it doesn't make it to the render method?

Laravel doesn't make it to:

public function render($request, Exception $exception)

So how are you meant to log errors? This seems wrong in so many ways.

public function render($request, Exception $exception)
{
    if (strlen($exception->getMessage()) > 0) {
        $agent = new Agent();
        $errorLog = new ErrorLog;
        $errorLog->error_message = $exception->getMessage();
        $errorLog->error_file = $exception->getFile();
        $errorLog->error_line = $exception->getLine();
        $errorLog->request_ip = $request->ip();
        $errorLog->request_url = $request->root();
        $errorLog->request_device = $agent->isDesktop() ? 'Desktop' : ($agent->isMobile() ? 'Mobile' : 'Tablet');
        $errorLog->request_system = $agent->platform() . ' ' . $agent->version($agent->platform());
        $errorLog->request_browser = $agent->browser();
        $errorLog->error_happened_to = (Auth::check() ? Auth::user()->username : 'Guest');
        $errorLog->save();
    }

    return parent::render($request, $exception);
}

Also, my .EVN file:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=*************************************************
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=************

3条回答 默认 最新

相关推荐 更多相似问题