1.拦截
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import com.*.reconciliation.common.ThreadUtil;
import com.*.SystemOperationLogInfo;
import com.*.reconciliation.service.SystemOperationLogService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@Aspect
@Component
public class DBLoggerAspect {
private ObjectMapper mapper;
@Autowired
public DBLoggerAspect(ObjectMapper mapper) {
this.mapper = mapper;
}
@Autowired
private SystemOperationLogService systemOperationLogService;
@Around("@annotation(dbLogger)")
public Object exec(ProceedingJoinPoint invocation, DBLogger dbLogger) throws Throwable {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
Object result = null;
Date startTime = new Date();
try {
result = invocation.proceed();
} catch (Exception e) {
throw e;
} finally {
Object obj = result;
Date endTime = new Date();
ThreadUtil.logPool.execute(()->{
HttpSession session = request.getSession();
String userName = (String) session.getAttribute("userName");
StringBuffer str = new StringBuffer();
SystemOperationLogInfo systemOperationLogInfo = new SystemOperationLogInfo();
systemOperationLogInfo.setOperationUser(userName);
systemOperationLogInfo.setOperationName(dbLogger.operationName());
systemOperationLogInfo.setRequestUrl(request.getRequestURI());
if(invocation.getArgs() != null) {
for (Object object : invocation.getArgs()) {
if (object instanceof MultipartFile
|| object instanceof ServletRequest
|| object instanceof ServletResponse
|| object instanceof FilterChain
) {
continue;
}
try {
str.append(mapper.writeValueAsString(object));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
systemOperationLogInfo.setRequestData(str.toString());
}
systemOperationLogInfo.setRequestStartTime(startTime);
systemOperationLogInfo.setRequestEndTime(endTime);
systemOperationLogInfo.setUsedTime(Integer.parseInt(""+(endTime.getTime() - startTime.getTime())));
JSONObject headerJson = new JSONObject();
Enumeration<String> headers = request.getHeaderNames();
while(headers.hasMoreElements()) {
String key = headers.nextElement();
String value = request.getHeader(key);
headerJson.put(key, value);
}
systemOperationLogInfo.setRequestHeader(headerJson.toString());
systemOperationLogInfo.setRequestMethod(request.getMethod());
try {
systemOperationLogInfo.setResponseData(mapper.writeValueAsString(obj));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
systemOperationLogInfo.setSystemName("accountcenter");
systemOperationLogService.save(systemOperationLogInfo);
});
}
return result;
}
2.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DBLogger {
String operationName();
}
3.使用
@DBLogger(operationName = "*列表查询")
}