public class RequestLogAspect {
@Autowired(required = false)
HttpServletRequest request;
private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Around(value = PointCut.CONTRLLER_POINTCUT)
public Object aroundController(ProceedingJoinPoint jp) throws Throwable {
log.info("****访****问****开****始***************************************************");
LogParam lp = new LogParam();
if (!lp.hasLogin) {
return jp.proceed();
}
log.info("[请求路径]==>{}", lp.getUrl());
log.info("[请求方式]==>{}", lp.getReqmethod());
log.info("[请求参数]==>{}", lp.getParams());
log.info("[请求时间]==>{}", lp.getStartDate());
log.info("[请求用户]==>{}", lp.getUserName());
Object result = jp.proceed();
RequestLog rl = recordLog(jp, lp, result);
log.info("[处理结果]==>{}", rl.getEntrance());
log.info("[访问耗时]==>{}", rl.getUseTime() + "毫秒");
log.info("****访****问****结****束***************************************************");
return result;
}
private RequestLog recordLog(JoinPoint joinPoint, LogParam logParam, Object result) {
RequestLog requestLog = new RequestLog();
requestLog.setId(getLogId());
requestLog.setUserId(logParam.getUserId());
requestLog.setUserName(logParam.getUserName());
requestLog.setIp(request.getRemoteHost());
requestLog.setUrl(gainUrl());
requestLog.setMethod(logParam.getReqmethod());
requestLog.setAction(getControllerMethodInfo(joinPoint));
requestLog.setParams(logParam.getParams());
String resultMessage = createResultMessage(result);
requestLog.setEntrance(resultMessage);
long et = System.currentTimeMillis();
long startTime = logParam.getStartTime().getTime();
requestLog.setStartTime(startTime);
requestLog.setEndTime(et);
long useTime = et - startTime;
requestLog.setUseTime(useTime);
LogQueue.put(requestLog);
return requestLog;
}
private String getLogId() {
Object logIdObj = MDC.get(StaticValue.SYSLOG_ID);
if (null == logIdObj) {
return SnowFlake.nextKey();
}
return String.valueOf(logIdObj);
}
private String createResultMessage(Object result) {
String handlResult = null;
if (null != result && result instanceof JsonResult) {
JsonResult jr = (JsonResult) result;
Context context = new Context().put("code", jr.getCode()).put("message", jr.getMessage());
handlResult = context.toString();
} else {
handlResult = "0";
}
return handlResult;
}
private String gainParams() {
Enumeration<String> parameterNames = this.request.getParameterNames();
Context context = new Context();
while (parameterNames.hasMoreElements()) {
String nextElement = parameterNames.nextElement();
String value = this.request.getParameter(nextElement);
if (isNotNull(value)) {
value = value.length() > 25 ? value.substring(0, 25) : value;
context.put(nextElement, value);
}
}
if (!context.isEmpty()) {
return context.toString();
}
return "";
}
private String gainUrl() {
return request.getRequestURI().replaceAll(request.getContextPath(), "");
}
public static String getControllerMethodInfo(JoinPoint joinPoint) {
Class<?> targetClass = joinPoint.getTarget().getClass();
String methodName = joinPoint.getSignature().getName();
Method[] methods = targetClass.getMethods();
for (Method method : methods) {
if (method.getName().equals(methodName)) {
SystemLog annotation = method.getAnnotation(SystemLog.class);
if (null != annotation) {
return annotation.value();
} else {
break;
}
}
}
return targetClass.getSimpleName() + "." + methodName;
}
class LogParam {
private String params;
private Date startTime;
private String startDate;
private User user;
private String url;
private String reqmethod;
private String userId;
private String userName;
private boolean hasLogin = true;
public LogParam() {
this.user = UserUtils.getLoginUser();
if (null == this.user) {
this.hasLogin = false;
}else{
this.userId = user.getId();
this.userName = user.getUserName();
}
this.startTime = new Date();
this.startDate = sdf.format(startTime);
this.url = request.getRequestURL().toString();
this.reqmethod = request.getMethod();
this.params = gainParams();
}
public boolean isHasLogin() {
return hasLogin;
}
public void setHasLogin(boolean hasLogin) {
this.hasLogin = hasLogin;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getParams() {
return params;
}
public void setParams(String params) {
this.params = params;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public WholeUser getUser() {
return user;
}
public void setUser(WholeUser user) {
this.user = user;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getReqmethod() {
return reqmethod;
}
public void setReqmethod(String reqmethod) {
this.reqmethod = reqmethod;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
}