【TP5】异常处理及日志管理

版权声明:咔咔 来自https://blog.csdn.net/fangkang7 https://blog.csdn.net/fangkang7/article/details/86523857

author:咔咔

wechat:fangkangfk

在控制器创建exception控制器,并重写render方法

<?php

namespace app\admin\controller;

use think\Log;
use Exception as Exceptionthink;
use think\exception\Handle;

class Exception extends Handle
{

    public function render(Exceptionthink $e)
    {

        $errorMessage = $e->getMessage();

        $errorFile = $e->getFile();

        $errorLine = $e->getLine();

        $errorTime = date('Y-m-d H-i-s',time());

        $data = [
            'Time'    => $errorTime,
            'Message' => $errorMessage,
            'File'    => $errorFile,
            'Line'    => $errorLine
        ];

        Log::init([
            'type' => 'file',
            'path' => ROOT_PATH . 'error_log/',
            'apart_level'   =>  ['error'],
            'max_files'	=> 7
        ]);

        Log::write($data);

        return redirect('admin/error/index');
    }

}

然后在配置文件配置,这里是单独给后台做的异常,所以没有在公共配置文件写。

 

猜你喜欢

转载自blog.csdn.net/fangkang7/article/details/86523857
tp5