215 lines
8.7 KiB
Java
215 lines
8.7 KiB
Java
package com.adc.da.Log;
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
import com.adc.da.slrs.sysLog.entity.SysLog;
|
|
import com.adc.da.slrs.sysLog.service.ISysLogService;
|
|
import com.adc.da.util.LoginUserUtil;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.google.gson.Gson;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.aspectj.lang.JoinPoint;
|
|
import org.aspectj.lang.annotation.AfterReturning;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.lang.reflect.Method;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.Executor;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
|
|
@Aspect
|
|
@Component
|
|
@Slf4j
|
|
public class logs {
|
|
//日志 注解方式存储日志
|
|
|
|
long beginTime;
|
|
|
|
|
|
@Autowired
|
|
ISysLogService sysLogService;
|
|
|
|
@Autowired(required = false)
|
|
private HttpServletRequest request;
|
|
|
|
// 配置织入点
|
|
@Pointcut("@annotation(EpcLog)")
|
|
public void logPointCut() {
|
|
log.info("=================已进入监听==========");
|
|
beginTime = System.currentTimeMillis();
|
|
}
|
|
|
|
|
|
/**
|
|
* 后置通知(在方法执行之后并返回数据) 用于拦截Controller层无异常的操作
|
|
* @param joinPoint 切点
|
|
*/
|
|
@AfterReturning(value = "logPointCut()",returning="returnValue")
|
|
public void after(JoinPoint joinPoint, Object returnValue){
|
|
Executor executor = Executors.newCachedThreadPool();
|
|
try {
|
|
String description = getControllerMethodInfo(joinPoint).get("description").toString();
|
|
String speclaiValue = getControllerMethodInfo(joinPoint).get("speclaiValue").toString();
|
|
String speclaiFlag = getControllerMethodInfo(joinPoint).get("speclaiFlag").toString();
|
|
Map<String, String[]> logParams = request.getParameterMap();
|
|
System.out.println(returnValue);
|
|
SysLog log = new SysLog();
|
|
// ##请求用户
|
|
log.setUserId(LoginUserUtil.getUserId());
|
|
// ##日志标题
|
|
log.setOperLocation(description);
|
|
// ##日志类型
|
|
log.setOper(String.valueOf(getControllerMethodInfo(joinPoint).get("type")));
|
|
// ##请求参数
|
|
StringBuilder stringBuilder=new StringBuilder();
|
|
for (String s:logParams.keySet()) {
|
|
if (!s.equals("_t")) {
|
|
stringBuilder.append("," + s + "=" + logParams.get(s)[0]);
|
|
}
|
|
}
|
|
log.setJson(stringBuilder.substring(1,stringBuilder.length()));
|
|
|
|
//特殊处理
|
|
if ("1".equals(speclaiFlag)){
|
|
//这个是国内或者海外的查询接口
|
|
String gson=new Gson().toJson(returnValue);
|
|
JSONObject jsonObject= JSON.parseObject(gson);
|
|
JSONObject object= JSON.parseObject(jsonObject.getString("data"));
|
|
if (null!=object.getString("standName") && !"".equals(object.getString("standName"))) {
|
|
log.setSpecial(object.getString("standSort") + " " + object.getString("standNumber") + "-" + object.getString("standYear") + " " + object.getString("standName") + " " + speclaiValue);
|
|
}else {
|
|
log.setSpecial(object.getString("standSort") + " " + object.getString("standNumber") + "-" + object.getString("standYear") + " " + object.getString("standEnName") + " " + speclaiValue);
|
|
}
|
|
}else if ("Invaliddownload1".equals(speclaiFlag) || "Invaliddownload2".equals(speclaiFlag) || "2".equals(speclaiFlag)){
|
|
//国内预览 下载 带水印下载
|
|
StringBuilder stringBuilder1=new StringBuilder();
|
|
for (String s:logParams.keySet()){
|
|
if (s.equals("standNumber")) {
|
|
stringBuilder1.append(logParams.get(s)[0]);
|
|
} else if (s.equals("standName")) {
|
|
stringBuilder1.append(" "+logParams.get(s)[0]);
|
|
}
|
|
}
|
|
stringBuilder1.append(" "+speclaiValue);
|
|
log.setSpecial(stringBuilder1.toString());
|
|
}else if ("Foreigndownload1".equals(speclaiFlag) || "Foreigndownload2".equals(speclaiFlag) || "3".equals(speclaiFlag)){
|
|
//海外预览 下载 带水印下载
|
|
StringBuilder stringBuilder1=new StringBuilder();
|
|
for (String s:logParams.keySet()){
|
|
if (s.equals("standNumber")) {
|
|
stringBuilder1.append(logParams.get(s)[0]);
|
|
} else if (s.equals("standName")) {
|
|
stringBuilder1.append(" "+logParams.get(s)[0]);
|
|
}
|
|
}
|
|
stringBuilder1.append(" "+speclaiValue);
|
|
log.setSpecial(stringBuilder1.toString());
|
|
}else if ("5".equals(speclaiFlag)){
|
|
//企标详情
|
|
String gson=new Gson().toJson(returnValue);
|
|
JSONObject jsonObject= JSON.parseObject(gson);
|
|
JSONObject object= JSON.parseObject(jsonObject.getString("data"));
|
|
log.setSpecial(object.getString("standCode")+" "+object.getString("standName")+" "+speclaiValue);
|
|
}else if ("Buss".equals(speclaiFlag)){
|
|
//企标下载 预览 带水印下载
|
|
StringBuilder stringBuilder1=new StringBuilder();
|
|
for (String s:logParams.keySet()){
|
|
if (s.equals("standCode")) {
|
|
stringBuilder1.append(logParams.get(s)[0]);
|
|
} else if (s.equals("standName")) {
|
|
stringBuilder1.append(" "+logParams.get(s)[0]);
|
|
}
|
|
}
|
|
|
|
log.setSpecial(stringBuilder1+" "+speclaiValue);
|
|
}
|
|
// ##请求开始时间
|
|
long endTime = System.currentTimeMillis();
|
|
// ##请求耗时
|
|
Long logElapsedTime = endTime - beginTime;
|
|
log.setRunTime(logElapsedTime.intValue());
|
|
log.setOperTime(new Date());
|
|
// ##调用线程保存至log表
|
|
executor.execute(new SaveSystemLogThread(log, sysLogService));
|
|
} catch (Exception e) {
|
|
log.error("AOP后置通知异常", e);
|
|
}
|
|
((ExecutorService) executor).shutdown();
|
|
}
|
|
|
|
/**
|
|
* 保存日志至数据库
|
|
*/
|
|
protected static class SaveSystemLogThread implements Runnable {
|
|
|
|
private SysLog log;
|
|
private ISysLogService logService;
|
|
|
|
public SaveSystemLogThread(SysLog esLog, ISysLogService logService) {
|
|
this.log = esLog;
|
|
this.logService = logService;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
log.setId(String.valueOf(UUID.randomUUID()));
|
|
logService.save(log);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取注解中对方法的描述信息 用于Controller层注解
|
|
* @param joinPoint 切点
|
|
* @return 方法描述
|
|
* @throws Exception
|
|
*/
|
|
public static Map<String, Object> getControllerMethodInfo(JoinPoint joinPoint) throws Exception{
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>(16);
|
|
// ## 获取目标类名
|
|
String targetName = joinPoint.getTarget().getClass().getName();
|
|
// ## 获取方法名
|
|
String methodName = joinPoint.getSignature().getName();
|
|
// ## 获取相关参数
|
|
Object[] arguments = joinPoint.getArgs();
|
|
// ## 生成类对象
|
|
Class targetClass = Class.forName(targetName);
|
|
// ## 获取该类中的方法
|
|
Method[] methods = targetClass.getMethods();
|
|
|
|
String description = "";
|
|
String type = null;
|
|
String speclaiValue = "";
|
|
String speclaiFlag = "";
|
|
|
|
for(Method method : methods) {
|
|
if(!method.getName().equals(methodName)) {
|
|
continue;
|
|
}
|
|
Class[] clazzs = method.getParameterTypes();
|
|
if(clazzs.length != arguments.length) {
|
|
// ## 比较方法中参数个数与从切点中获取的参数个数是否相同,原因是方法可以重载
|
|
continue;
|
|
}
|
|
description = method.getAnnotation(EpcLog.class).description();
|
|
type = method.getAnnotation(EpcLog.class).businessType().name();
|
|
speclaiValue = method.getAnnotation(EpcLog.class).speclaiValue();
|
|
speclaiFlag = method.getAnnotation(EpcLog.class).speclaiFlag();
|
|
map.put("description", description);
|
|
map.put("type", type);
|
|
map.put("speclaiValue",speclaiValue);
|
|
map.put("speclaiFlag",speclaiFlag);
|
|
}
|
|
return map;
|
|
}
|
|
|
|
}
|