表设计
CREATE TABLE `app_error_log` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`message` varchar(1000) NOT NULL DEFAULT '' COMMENT '错误信息',
`file` varchar(255) NOT NULL DEFAULT '' COMMENT '错误文件',
`class` varchar(255) NOT NULL DEFAULT '' COMMENT '错误类',
`line` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '错误行数',
`method` varchar(20) NOT NULL DEFAULT '' COMMENT '请求方式',
`request_route` varchar(255) NOT NULL DEFAULT '' COMMENT '访问的路由',
`request_header` varchar(2000) NOT NULL DEFAULT '' COMMENT '请求头',
`request_get` varchar(2000) NOT NULL COMMENT 'GET参数',
`request_post` text COMMENT 'POST参数',
`request_put` text COMMENT 'PUT参数',
`trace` text COMMENT '报错详情',
`create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '请求时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='异常错误日志表';
## laravel
处理方式
# \App\Exceptions\Handler::render
public function render($request, Throwable $exception)
{
// ...
$this->mini_api_log($request, $exception);
// ...
}
protected function mini_api_log(Request $request, \Throwable $exception)
{
$request_put = json_encode($request->input(), JSON_UNESCAPED_UNICODE);
if (empty($request_put)) {
$request_put = file_get_contents("php://input");
}
$request_post = $request->post();
if (empty($request_post)) {
$request_post = $_POST;
}
$log = [
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'class' => get_class($exception),
'line' => $exception->getLine(),
'method' => $request->getMethod(),
'request_route' => $request->getRequestUri(),
'request_header' => json_encode($request->header(), JSON_UNESCAPED_UNICODE),
'request_get' => json_encode($request->query(), JSON_UNESCAPED_UNICODE),
'request_post' => json_encode($request_post, JSON_UNESCAPED_UNICODE),
'request_put' => json_encode($request_put, JSON_UNESCAPED_UNICODE),
'trace' => $exception->getTraceAsString(),
'create_time' => time()
];
try {
DB::table("bs_log_api_error")->insertGetId($log);
} catch (\Throwable $e) {
}
}
# thinkphp部分
# config.php 配置文件里面,接管框架的异常处理类
public function render(\Exception $e)
{
// 当关闭调试模式时,通过钉钉群机器人通知
if (!Env::get('app.debug')) {
}
$this->exception_log($e);
return parent::render($e);
}
public function exception_log(\Exception $e)
{
$traces = $e->getTrace();
$file = $e->getFile();
$line = $e->getLine();
foreach ($traces as $trace) {
if (isset($trace['file']) && strpos($trace['file'], 'api_mini_apps') !== false) {
$file = $trace['file'];
$line = $trace['line'];
break;
}
}
$route = request()->path();
$log = [
'message' => $e->getMessage(),
'file' => $file,
'class' => get_class($e),
'line' => $line,
'trace' => json_encode($traces, 256),
'method' => request()->method(true),
'request_route' => $route,
'request_header' => !empty(request()->header()) ? json_encode(request()->header(), 256) : '',
'request_get' => !empty(request()->get()) ? json_encode(request()->get(), 256) : '',
'request_post' => !empty(request()->post()) ? json_encode(request()->post(), 256) : '',
'request_put' => !empty(request()->put()) ? json_encode(request()->put(), 256) : "",
'create_time' => request()->server('REQUEST_TIME'),
];
db('bs_app_api_error_log')->insert($log);
}