/**
* 发送邮件错误日志
* @param $method 方法
* @param $params 所调用方法的参数
* @param $result crm接口返回结果
*/
public static function sendErrorMail($method, $params, $result)
{
$method = $method;
$params = var_export($params, true);
$result = var_export($result, true);
$errTotal = Cache::get('crmTotal', 1);
$message = "接口调用失败.
失败次数:$errTotal
接口名:$method
请求参数:$params
返回结果:$result
";
$toUsers = [
];
if(Cache::get('sendMailTime',0) + 60 < time()){
Cache::forever('sendMailTime', time());
Cache::forever('crmTotal', 1);
Mail::raw($message,function ($mail)use($toUsers){
$mail->to('aicken.peng@etocrm.com')->subject('CC接口错误报警');
foreach($toUsers as $user){
$mail->cc($user);
}
});
}else{
Cache::increment('crmTotal');
}
}
//记录错误日志方法
public static function writelog($method, $data, $result , $use_time)
{
$file_name = storage_path('/logs/crm/'.date('Y-m-d').'.log');
if(!file_exists($file_name)){
mkdir($file_name, 0777, true);
}
$strLog = '';
foreach(array_merge([date('Y-m-d H:i:s')], func_get_args()) as $str) {
if(is_array($str)) {
$strLog .= "( " . implode("\t", $str) . " ) \t";
} else {
$strLog .= $str . "\t";
}
}
file_put_contents($file_name, $strLog . "\n", FILE_APPEND);
}
laravel框架的异常处理主要靠app/Exceptions/Handler.php处理。重写这个类的两个方法即可
public function report(Exception $exception)
{
if(empty(env('APP_DEBUG')) && $exception->getMessage()){//判断是否开启调试模式,以及是否有错误信息
\Mail::raw($exception . 'server:' . json_encode(\Request::server()), function ($m) {
$m->subject('CC错误监控');
$m->to('aicken.peng@etocrm.com');
});
}
parent::report($exception);//报告错误
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if(env('APP_DEBUG')){//判断是否开启调试模式
return parent::render($request, $exception);
}
$msg = empty($exception->getMessage()) ? $exception->getMessage() : '好像出错啦!';
if($request->ajax()){//判断是否为ajax请求
return response()->json(\Tool::outErrcode('99', $msg));
}else{//直接抛出错误页面
return parent::render($request, $exception);
}
}