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

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条回答 默认 最新

  • douxianji3367 2017-09-29 08:35
    关注

    This is how I handle the same issue. In app/Exceptions/Handler.php:

    public function report(Exception $exception)
    {
        if(!config('app.debug')) {
            if ($this->shouldReport($exception)) {
                $this->logError($exception);
            }
        }
        if(env('APP_ENV') == 'local'){
            parent::report($exception);
        }
    }
    
    public function render($request, Exception $exception)
    {
        if(!config('app.debug')) {
            if($this->shouldReport($exception)){
                return response()->view('errors.500', compact('exception'));
            }
        }
        return parent::render($request, $exception);
    }
    

    I added a function logError that writes the error to the database, and I have a template in resources/views/errors - 500.blade.php - that has a custom error page.

    I also use the APP_DEBUG in the .env to determine whether to log the error to the database and display the error page or to show the error details on screen.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?