|
@@ -0,0 +1,158 @@
|
|
|
+package com.iden.common.logaspect;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+
|
|
|
+import com.iden.common.entity.IdenSysLog;
|
|
|
+import com.iden.common.service.IdenSysLogService;
|
|
|
+
|
|
|
+import com.iden.common.util.StringUtil;
|
|
|
+import com.iden.common.util.WebPageUtils;
|
|
|
+import com.iden.common.vo.UserLoginedConvertVO;
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.annotation.After;
|
|
|
+import org.aspectj.lang.annotation.AfterThrowing;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+
|
|
|
+@Service
|
|
|
+@Aspect
|
|
|
+public class LogAspect {
|
|
|
+ private static final Logger logger = LogManager.getLogger(LogAspect.class);
|
|
|
+ @Resource
|
|
|
+ private IdenSysLogService idenSysLogService;
|
|
|
+
|
|
|
+ @Pointcut("@annotation(com.iden.common.logaspect.LogAnnotation)")
|
|
|
+ public void logAspect() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @After("logAspect()")
|
|
|
+ public void doBefore(JoinPoint jp) {
|
|
|
+ doSaveLog(jp, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterThrowing(pointcut = "logAspect()", throwing = "t")
|
|
|
+ public void doAfterThrowing(JoinPoint jp, Throwable t) {
|
|
|
+ doSaveLog(jp, t);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Method getTargetMethod(JoinPoint jp) {
|
|
|
+ String methodName = jp.getSignature().getName();
|
|
|
+ Object[] args = jp.getArgs();
|
|
|
+ Method[] methods = jp.getTarget().getClass() == null ? new Method[0] : jp.getTarget().getClass().getDeclaredMethods();
|
|
|
+ for (Method method : methods) {
|
|
|
+ if (method.getName().equals(methodName)
|
|
|
+ && method.isAnnotationPresent(LogAnnotation.class)) {
|
|
|
+ Class<?>[] clazz = method.getParameterTypes();
|
|
|
+ if (clazz.length == args.length) {
|
|
|
+ return method;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void doSaveLog(JoinPoint jp, Throwable t) {
|
|
|
+
|
|
|
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ HttpServletRequest request = requestAttributes.getRequest();
|
|
|
+
|
|
|
+ Method method = getTargetMethod(jp);
|
|
|
+ if (method == null) {
|
|
|
+ logger.warn("没有找到匹配的方法名:"
|
|
|
+ + jp.getSignature().getName()
|
|
|
+ + '(' + StringUtil.join(jp.getArgs(), ',') + ')');
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
|
|
|
+
|
|
|
+ String moduleName = logAnnotation.moduleName();
|
|
|
+ String description = logAnnotation.description();
|
|
|
+ //String uri = request.getRequestURI();
|
|
|
+ //String ctx = request.getContextPath();
|
|
|
+ //String opUrl = StringUtil.substringAfter(uri, ctx);
|
|
|
+
|
|
|
+ Object[] args = jp.getArgs();
|
|
|
+
|
|
|
+ Map<String, Object> params = Maps.newHashMap();
|
|
|
+ for (int i = 0, len = args.length; i < len; ++i) {
|
|
|
+ params.put("p" + (i + 1), args[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("用户登录".equals(moduleName)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ IdenSysLog idenSysLog = new IdenSysLog();
|
|
|
+
|
|
|
+ idenSysLog.setContent(description);
|
|
|
+ //idenSysLog.setModuleName(moduleName);
|
|
|
+ idenSysLog.setLogType(logAnnotation.type().getValue());
|
|
|
+ // idenSysLog.setOpUrl(opUrl);
|
|
|
+ List<Object> argList = Lists.newArrayList();
|
|
|
+ for (Object arg : args) {
|
|
|
+ if (arg instanceof HttpServletRequest
|
|
|
+ || arg instanceof HttpServletResponse) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (arg instanceof MultipartFile) {
|
|
|
+ argList.add(((MultipartFile)arg).getOriginalFilename());
|
|
|
+ } else {
|
|
|
+ argList.add(arg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //idenSysLog.setOpParams(JSONUtil.toJsonStr(argList));
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ UserLoginedConvertVO loginUser = WebPageUtils.getCurrentLoginedUser(request);
|
|
|
+ if (loginUser != null){
|
|
|
+ idenSysLog.setUsername(loginUser.getUsername());
|
|
|
+ } else {
|
|
|
+ logger.error("记录日志出现错误:token为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("记录日志出现错误:" + e.getMessage(), e);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (logger.isDebugEnabled()) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("\n类名:").append(jp.getTarget().getClass().getName())
|
|
|
+ .append("\n方法名:").append(method.getName())
|
|
|
+ .append("\n参数:").append(JSONUtil.toJsonStr(argList))
|
|
|
+ .append("\n日志对象:").append(JSONUtil.toJsonStr(idenSysLog));
|
|
|
+ logger.debug(sb.toString());
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String ip = WebPageUtils.getIpAddr(request);
|
|
|
+ idenSysLog.setIp(ip);
|
|
|
+ idenSysLog.setCreateTime(new Date());
|
|
|
+ idenSysLogService.save(idenSysLog);
|
|
|
+ } catch (Throwable tx) {
|
|
|
+ logger.error("记录日志出现错误:" + tx.getMessage(), tx);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|