几个常用的切点注解,这次使用了@Before和@Around
1.@Before 前置增强(目标方法执行之前,执行注解标注的内容)
2.@AfterReturning 后置增强(目标方法正常执行完毕后,执行)
3.@Around 环绕增强(目标方法执行前后,分别执行一些代码)
4.@AfterThrowing 抛出增强(目标方法发生异常,执行)
5.@After Final增强(不管是抛出异常还是正常退出,该增强都会得到执行。一般用于释放资源,相当于try{}finally{})。
切Controller打印请求的接口、参数、返回值以及耗时情况。
package com.tfjy.arbackend.aop;
import com.alibaba.fastjson.JSONObject;
import com.tfjy.arbackend.util.FrontResult;
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.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@Aspect
@Component
public class RestAopConfig {
public static Boolean onOff = false;
private static Log logger = LogFactory.getLog(RestAopConfig.class);
@Pointcut("execution(public * com.tfjy.arbackend.controller..*.*(..))")
public void pointCutRestDef(){
}
//环绕切点
@Around("pointCutRestDef()")
public Object processRst(ProceedingJoinPoint point) throws Throwable{
Object returnValue = null;
final List<Object> params = new ArrayList<>();
//获得请求信息
ServletRequestAttributes sra =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if(sra==null){
return point.proceed();
}
HttpServletRequest request = sra.getRequest();
Object[] args = point.getArgs();
//过滤出HttpServlet
for (int i = 0; i < args.length; i++) {
Object object = args[i];
if (object instanceof HttpServletResponse){
continue;
}
if (object instanceof HttpServletRequest){
continue;
}
params.add(object);
}
logger.info(
"rest method:——>"+point.getSignature().getDeclaringTypeName()+"."+point.getSignature().getName());
String cloneParams = JSONObject.toJSONString(params);
logger.info("rest param:——>"+cloneParams);
long startTime = System.currentTimeMillis();
//去执行方法,这里的异常交给统一捕获异常去处理
returnValue = point.proceed(point.getArgs());
//处理返回值
if (returnValue instanceof FrontResult){
FrontResult frontResult=(FrontResult)returnValue;
logger.info("rest response:——>"+frontResult.toString());
}
long endTime = System.currentTimeMillis();
logger.info("rest"+request.getRequestURI()+"----used time----"+(endTime - startTime));
Boolean boolean1 =true;
if (returnValue != null && !returnValue.equals(boolean1)){
logger.info(JSONObject.toJSONString(returnValue));
}
return returnValue;
}
}
切Service打印日志,URL,请求方式,IP,类名,方法
package com.tfjy.arbackend.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
@Aspect //注解将一个java类定义为切面类
@Component
public class AopGetService {
private static Log logger = LogFactory.getLog(AopGetService.class);
@Pointcut("execution(public * com.tfjy.arbackend.service..*.*(..))")//切入点描述 这个是service包的切入点
public void getServiceJournal() {
}//签名,可以理解成这个切入点的一个名称
//前置切点
@Before("getServiceJournal()")//在切入点开始处切入内容
public void logBeforeService(JoinPoint joinPoint) {
// 接收到请求,记录请求内容
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if(requestAttributes==null){
return ;
}
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
// 记录下请求内容
logger.info("URL : " + request.getRequestURL().toString());
logger.info("HTTP_METHOD : " + request.getMethod());
logger.info("IP : " + request.getRemoteAddr());
//下面这个getSignature().getDeclaringTypeName()是获取包+类名的 然后后面的joinPoint.getSignature.getName()获取了方法名
logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
}
}
总结
到此这篇关于java通过AOP实现全局日志打印的文章就介绍到这了,更多相关 AOP全局日志打印内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!