package com.shuidi.aop;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.shuidi.common.util.LoggerUtil;
/**
* Aop统一日志管理
* @author 李修睿
* @Date 2019年8月22日
*/
@Aspect
@Component
public class LogResult {
/**
* 保存信息到当前线程
*/
public static final ThreadLocal
String method= signature.getDeclaringTypeName() + "." +signature.getName();
String args=Arrays.toString(joinPoint.getArgs());
Map
map.put("method", method);
map.put("args", args);
INFO.set(map);
// 记录下请求内容
log.info("URL : " + request.getRequestURL().toString());
log.info("HTTP_METHOD : " + request.getMethod());
log.info("IP : " + request.getRemoteAddr());
log.info("CLASS_METHOD : " +method);
log.info("ARGS : " + args);
}
/**
* 日志记录返回结果
* @param ret
* @throws Throwable
*/
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
INFO.remove();
// 处理完请求,返回内容
if(null==ret){
log.info("返回结果 : null" );
}else{
log.info("返回结果 : " +ret.toString() );
}
}
}
package com.shuidi.common.result;
import com.alibaba.fastjson.JSON;
/**
* 统一返回模型
* @author 李修睿
* @Date 2019年8月22日
* @param
*/
public class Result
private int code;
private String message;
private T datas;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getDatas() {
return datas;
}
public void setDatas(T datas) {
this.datas = datas;
}
public String toString(){
return JSON.toJSONString(this);
}
}
package com.shuidi.common.result;
import java.util.List;
import com.shuidi.common.myException.CustomException;
public class ResultUtil{
public static final String CODE_REDBAG_CANT_BUY = "-1";
public static final int CODE_SUCCESS = 1;
public static final int CODE_FAIL = -1;
public static final int CODE_NULL = 0;
public static final String MSG_SUCCESS = "成功";
public static final String MSG_FAIL = "失败";
public static final String MSG_NULL = "无记录";
public static final String MSG_ERROR = "未知错误";
public static final int DB_RETURN_FAIL = 0;
/**
* 正常返回
* @param t
* @return
*/
public static
Result
if(null==t){
res.setCode(CODE_NULL);
res.setMessage(MSG_NULL);
}else if(t instanceof List){
if(((List>) t).isEmpty()){
res.setCode(CODE_NULL);
res.setMessage(MSG_NULL);
}else{
res.setCode(CODE_SUCCESS);
res.setMessage(MSG_SUCCESS);
}
}else{
res.setCode(CODE_SUCCESS);
res.setMessage(MSG_SUCCESS);
}
res.setDatas(t);
return res;
}
/**
* 返回int
* @param t
* @return
*/
public static Result
Result
if(t<1){
res.setCode(CODE_NULL);
res.setMessage(MSG_FAIL);
}else{
res.setCode(CODE_SUCCESS);
res.setMessage(MSG_SUCCESS);
}
res.setDatas(t);
return res;
}
/**
* 返回Sring
* @param t
* @return
*/
public static Result
Result
if(null==t||"".equals(t)){
res.setCode(CODE_NULL);
res.setMessage(MSG_FAIL);
}else{
res.setCode(CODE_SUCCESS);
res.setMessage(MSG_SUCCESS);
}
res.setDatas(t);
return res;
}
/**
* 返回对象
* @param t
* @return
*/
public static
Result
res.setCode(code);
res.setMessage(msg);
res.setDatas(t);
return res;
}
/**
* 返回异常
* @param t
* @return
*/
public static
Result
if(t instanceof CustomException){//返回自定义异常
CustomException c =( CustomException)t;
res.setCode(c.getCode());
res.setMessage(c.getMessage());
}else{//返回系统异常
res.setCode(CODE_FAIL);
res.setMessage(MSG_ERROR);
}
return res;
}
}
package com.shuidi.exceptionhandler;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.shuidi.aop.LogResult;
import com.shuidi.common.myException.CustomException;
import com.shuidi.common.result.Result;
import com.shuidi.common.result.ResultUtil;
/**
* controller 增强器
*
* @author sam
* @since 2017/7/17
*/
@ControllerAdvice
public class MyControllerAdvice {
private static final Logger log= LoggerFactory.getLogger(MyControllerAdvice.class);
/**
* 全局异常捕捉处理
* @param ex
* @return
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public Result
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
log.error("URL:"+request.getRequestURI());
log.error("HTTP_METHOD : " + request.getMethod());
log.error("IP : " + request.getRemoteAddr());
Map
if(null!=map){
log.error("CLASS_METHOD : " +map.get("method"));
log.error("ARGS : " + map.get("args"));
LogResult.INFO.remove();
}
log.error("CaseBy : " + ex.getCause());
Result
log.error("返回结果:"+res.toString(),ex);
return res;
}
/**
* 拦截捕捉自定义异常 MyException.class
* @param ex
* @return
*/
@ResponseBody
@ExceptionHandler(value = CustomException.class)
public Result
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
log.error("URL:"+request.getRequestURI());
log.error("HTTP_METHOD : " + request.getMethod());
log.error("IP : " + request.getRemoteAddr());
Map
if(null!=map){
log.error("CLASS_METHOD : " +map.get("method"));
log.error("ARGS : " + map.get("args"));
LogResult.INFO.remove();
}
log.error("CaseBy : " + ex.getCause());
Result
log.error("返回结果:"+res.toString());
return res;
}
}
package com.shuidi.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggerUtil {
public static Logger getLogger(Class> clazz){
return LoggerFactory.getLogger(clazz);
}
}