1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Map; import javassist.ClassClassPath; import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; import javassist.bytecode.CodeAttribute; import javassist.bytecode.LocalVariableAttribute; import javassist.bytecode.MethodInfo; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.glodon.bim5d.controller.BaseController; import com.glodon.bim5d.util.LoggerMsgUtil; @Aspect @Component public class LogAspect { @Pointcut ( "execution(public void xxx.xxx.xxx.controller.QRCodeController.download(..))" ) public void pointCut() { } @Before ( "pointCut()" ) public void before(JoinPoint joinPoint) throws Exception { //获得代理类 BaseController controller = (BaseController)joinPoint.getTarget(); //获得request HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); String uri = request.getRequestURI(); String queryString = request.getQueryString(); String projectId = getProjectId(joinPoint); try { HashMap<String, Object> inputParam = new HashMap<String, Object>(); inputParam.put( "uri" , uri); inputParam.put( "projectId" , projectId); inputParam.put( "RequestBody" , queryString); LoggerMsgUtil.enterMethodInfo(controller.getLogger(), inputParam); } catch (Exception e) { LoggerMsgUtil.Info(controller.getLogger(), "记录入口参数失败:{0}" , e.getMessage()); } } /** * 从参数列表中获取projectId * */ private String getProjectId(JoinPoint joinPoint){ String projectId = null ; try { //获取参数名称和值 Map<String,Object> nameAndArgs = getFieldsNameValueMap(joinPoint); projectId = nameAndArgs.get( "projectId" ) == null ? null : String.valueOf(nameAndArgs.get( "projectId" )); } catch (Exception e) { } return projectId; } private Map<String,Object> getFieldsNameValueMap(JoinPoint joinPoint) throws Exception { Object[] args = joinPoint.getArgs(); String classType = joinPoint.getTarget().getClass().getName(); Class<?> clazz = Class.forName(classType); String clazzName = clazz.getName(); String methodName = joinPoint.getSignature().getName(); //获取方法名称 Map<String,Object > map= new HashMap<String,Object>(); ClassPool pool = ClassPool.getDefault(); ClassClassPath classPath = new ClassClassPath( this .getClass()); pool.insertClassPath(classPath); CtClass cc = pool.get(clazzName); CtMethod cm = cc.getDeclaredMethod(methodName); MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag); if (attr == null ) { throw new RuntimeException(); } int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1 ; for ( int i = 0 ; i < cm.getParameterTypes().length; i++){ map.put( attr.variableName(i + pos),args[i]); //paramNames即参数名 } return map; } } |