¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.lf.server.aspect; |
| | | |
| | | import com.lf.server.annotation.SysLog; |
| | | import com.lf.server.entity.sys.OperateEntity; |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | import com.lf.server.helper.WebHelper; |
| | | import com.lf.server.service.sys.OperateService; |
| | | import com.lf.server.service.sys.TokenService; |
| | | import io.swagger.annotations.Api; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.aspectj.lang.ProceedingJoinPoint; |
| | | import org.aspectj.lang.annotation.Around; |
| | | import org.aspectj.lang.annotation.Aspect; |
| | | import org.aspectj.lang.annotation.Pointcut; |
| | | import org.aspectj.lang.reflect.MethodSignature; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.lang.reflect.Method; |
| | | |
| | | /** |
| | | * æ¥å¿åé¢ç±» |
| | | * @author WWW |
| | | */ |
| | | @Aspect |
| | | @Component |
| | | public class LogAspect { |
| | | @Autowired |
| | | private TokenService tokenService; |
| | | |
| | | @Autowired |
| | | private OperateService operateService; |
| | | |
| | | private final static String SELECT = "/select"; |
| | | |
| | | private final static String INSERT = "/insert"; |
| | | |
| | | private final static String UPDATE = "/update"; |
| | | |
| | | private final static String DELETE = "/delete"; |
| | | |
| | | private final static String UPLOAD = "/upload"; |
| | | |
| | | private final static String DOWNLOAD = "/download"; |
| | | |
| | | private final static String COUNT = "/count"; |
| | | |
| | | private final static String SPLIT = "\\\\"; |
| | | |
| | | private static final Log log = LogFactory.getLog(LogAspect.class); |
| | | |
| | | @Pointcut("@annotation(com.lf.server.annotation.SysLog)") |
| | | public void logPointCut() { |
| | | // |
| | | } |
| | | |
| | | /** |
| | | * ç¯ç»éç¥ @Aroundï¼ä¹å¯ä»¥ä½¿ç¨ @Before (åç½®éç¥) æ @After (åç½®éç¥) |
| | | */ |
| | | @Around("logPointCut()") |
| | | public Object around(ProceedingJoinPoint point) throws Throwable { |
| | | long beginTime = System.currentTimeMillis(); |
| | | |
| | | // æ§è¡æ¹æ³ |
| | | Object result = point.proceed(); |
| | | |
| | | // æ§è¡æ¶é¿(毫ç§) |
| | | long time = System.currentTimeMillis() - beginTime; |
| | | |
| | | // ä¿åæ¥å¿ |
| | | saveLog(point, time); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * ä¿åæ¥å¿ |
| | | */ |
| | | private void saveLog(ProceedingJoinPoint joinPoint, long time) { |
| | | try { |
| | | MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
| | | Class<?> clazz = joinPoint.getTarget().getClass(); |
| | | Method method = signature.getMethod(); |
| | | |
| | | HttpServletRequest req = WebHelper.getRequest(); |
| | | String url = req.getServletPath(); |
| | | |
| | | OperateEntity oe = new OperateEntity(); |
| | | oe.setIp(WebHelper.getIpAddress(req)); |
| | | oe.setUrl(url); |
| | | oe.setExec(time); |
| | | |
| | | // 设置模å |
| | | Api api = clazz.getAnnotation(Api.class); |
| | | if (api != null) { |
| | | String[] strs = api.tags()[0].split(SPLIT); |
| | | oe.setModular1(strs[0]); |
| | | oe.setModular2(strs[1]); |
| | | } |
| | | |
| | | // 设置类å\æ¹æ³åãç±»å« |
| | | oe.setClazz(clazz.getName() + "." + method.getName()); |
| | | oe.setType(getType(method.getName())); |
| | | |
| | | // è®¾ç½®å¤æ³¨ |
| | | SysLog sysLog = method.getAnnotation(SysLog.class); |
| | | if (sysLog != null) { |
| | | oe.setBak(sysLog.value()); |
| | | } |
| | | |
| | | UserEntity ue = tokenService.getCurrentUser(req); |
| | | if (ue != null) { |
| | | oe.setUserid(ue.getId()); |
| | | } |
| | | |
| | | operateService.insertOperate(oe); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åæä½ç±»å |
| | | */ |
| | | private static int getType(String methodName) { |
| | | if (methodName.contains(SELECT)) { |
| | | return 1; |
| | | } |
| | | if (methodName.contains(INSERT)) { |
| | | return 2; |
| | | } |
| | | if (methodName.contains(UPDATE)) { |
| | | return 3; |
| | | } |
| | | if (methodName.contains(DELETE)) { |
| | | return 4; |
| | | } |
| | | if (methodName.contains(UPLOAD)) { |
| | | return 5; |
| | | } |
| | | if (methodName.contains(DOWNLOAD)) { |
| | | return 6; |
| | | } |
| | | // methodName.indexOf(COUNT) > -1 |
| | | if (methodName.contains(COUNT)) { |
| | | return 7; |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | } |