当前位置:首页 > 其他信息
新浪网 如何指导 

Spring使用前置切面和后置切面获取日志

e层楼   发布时间:2019-10-24   发布者:yangguang

package bootdomonaspect;

import bootdomonutilsHttpContextUtils;
import aspectjlangJoinPoint;
import aspectjlangProceedingJoinPoint;
import aspectjlangannotation*;
import aspectjlangreflectMethodSignature;
import slf4jLogger;
import slf4jLoggerFactory;
import springframeworkstereotypeponent;
import springframeworkwebcontextrequestRequestContextHolder;
import springframeworkwebcontextrequestServletRequestAttributes;
import sunutilIPAddressUtil;

import jaxservlethttpHttpServletRequest;
import jalangreflectMethod;
import jautilArrays;

@Aspect
@ponent
public class WebLogAspect {

private static final Logger logger = LoggerFactorygetLogger(WebL医疗器械ogAspectclass);

@Pointcut("execution( * bootdocontroller**())")//两个代表所有子目录,最后括号里的两个代表所有参数
public void logPointCut() {
}


@Before("logPointCut()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHoldergetRequestAttributes();
HttpServletRequest request = attributesgetRequest();

// 记录下请求内容
loggerinfo("请求地址 : " + requestgetRequestURL()toString());
loggerinfo("HTTP METHOD : " + requestgetMethod());
// 获取真实的ip地址
//loggerinfo("IP : " + IPAddressUtilgetClientIpAddress(request));
loggerinfo("CLASS_METHOD : " + joinPointgetSignature()getDeclaringTypeName() + ""
+ joinPointgetSignature()getName());
loggerinfo("参数 : " + ArraystoString(joinPointgetArgs()));
// logggerinfo("参数 : " + joinPointgetArgs());

}

@AfterReturning(returning = "ret", pointcut = "logPointCut()")// returning的值和doAfterReturning的参数名一致
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容(返回值太复杂时,打印的是物理存储空间的地址)
loggerdebug("返回值 : " + ret);
}

@Around("logPointCut()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long startTime = SystemcurrentTimeMillis();
Object ob = pjpproceed();// ob 为方法的返回值
loggerinfo("耗时 : " + (SystemcurrentTimeMillis() - startTime));
return ob;
}
}




Spring使用前置切面和后置切面获取日志评论(共有 0 条评论)

我要点评Spring使用前置切面和后置切面获取日志
用户名: 密码: (游客无需填写密码)

验证码: 看不清楚,点击刷新 (如果提示验证码过期,请点击验证码刷新。)
【评论提交后需要审核】