java aop通用审计日志_AOP统一日志打印处理(系统操作日志通用设计)

packagecom.xinyartech.erp.system.aop;importjava.lang.reflect.Modifier;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.annotation.AfterReturning;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.springframework.stereotype.Component;importcom.alibaba.fastjson.JSON;importcom.xinyartech.erp.core.util.Util;importjavassist.ClassClassPath;importjavassist.ClassPool;importjavassist.CtClass;importjavassist.CtMethod;importjavassist.NotFoundException;importjavassist.bytecode.CodeAttribute;importjavassist.bytecode.LocalVariableAttribute;importjavassist.bytecode.MethodInfo;/*** 通过spring aop实现service方法执行时间监控

*

*@authorLynch

**/@Aspect

@Componentpublic classWebLogAop {private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WebLogAop.class);private static ThreadLocal threadLocal = new ThreadLocal();//public static final String POINT = "execution (* com.xinyartech.erp.*.web.*.*(..))";

@Pointcut("(execution (* com.xinyartech.erp.*.web.*.*(..)))")public voidwebLog(){

}/*** 前置通知

*@paramjoinPoint 切点

*@throwsThrowable 异常*/@Before("webLog()")public void doBefore(JoinPoint joinPoint) throwsThrowable {

String uuid=Util.getUUID();

threadLocal.set(uuid);

String classType=joinPoint.getTarget().getClass().getName();

Class> clazz =Class.forName(classType);

String clazzName=clazz.getName();

log.info(String.format("[%s] 类名:%s", uuid, clazzName));

String methodName=joinPoint.getSignature().getName();

log.info(String.format("[%s] 方法名:%s", uuid, methodName));

String[] paramNames= getFieldsName(this.getClass(), clazzName, methodName);

Object[] args=joinPoint.getArgs();for(int k=0; k

log.info("[" + uuid + "] 参数名:" + paramNames[k] + ",参数值:" +JSON.toJSONString(args[k]));

}

}/*** 后置通知

* 打印返回值日志

*@paramret 返回值

*@throwsThrowable 异常*/@AfterReturning(returning= "ret", pointcut = "webLog()")public void doAfterReturning(JoinPoint joinPoint, Object ret) throwsThrowable {

String uuid=threadLocal.get();

String classType=joinPoint.getTarget().getClass().getName();

Class> clazz =Class.forName(classType);

String clazzName=clazz.getName();

log.info(String.format("[%s] 类名:%s", uuid, clazzName));

String methodName=joinPoint.getSignature().getName();

log.info(String.format("[%s] 方法名:%s", uuid, methodName));

log.info(String.format("[%s] 返回值 : %s", uuid, JSON.toJSONString(ret)));

log.info("*****************************************");

}/*** 得到方法参数的名称

*@paramcls 类

*@paramclazzName 类名

*@parammethodName 方法名

*@return参数名数组

*@throwsNotFoundException 异常*/

private static String[] getFieldsName(Class> cls, String clazzName, String methodName) throwsNotFoundException {

ClassPool pool=ClassPool.getDefault();

ClassClassPath classPath= newClassClassPath(cls);

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);

String[] paramNames= newString[cm.getParameterTypes().length];int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;for (int i = 0; i < paramNames.length; i++){

paramNames[i]= attr.variableName(i + pos); //paramNames即参数名

}returnparamNames;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值