AOP切面记录日志,并计算请求接口耗时
借鉴:https://blog.csdn.net/vtopqx/article/details/79917305package com.mortals.iot.framework.aspect;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.Http
·
借鉴:https://blog.csdn.net/vtopqx/article/details/79917305
package com.mortals.iot.framework.aspect;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.mortals.framework.ap.SysConstains;
import com.mortals.framework.util.HttpUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import lombok.extern.slf4j.Slf4j;
@Aspect
@Slf4j
@Component
public class AccessLogAspect {
@Pointcut("execution(public * com.xxx.iot..*Controller.*(..))")
public void accessLog() {
}
/* @Before("accessLog()")
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// url
log.info("ip[{}]url[{}]", request.getRemoteAddr(), request.getRequestURL());
// ags
if (joinPoint != null && joinPoint.getArgs() != null) {
StringBuffer buf = new StringBuffer("[");
for (int i = 0; i < joinPoint.getArgs().length; i++) {
Object arg = joinPoint.getArgs()[i];
if (arg != null && !(arg instanceof ServletRequest) && !(arg instanceof ServletResponse)) {
String argStr = arg.toString();
//针对登录密码日志,做特殊处理
if (argStr.contains("password") && argStr.contains("LoginForm")){
int pwIndex = argStr.indexOf("password=");
argStr = argStr.substring(0,pwIndex+10)+"****"+argStr.substring(argStr.indexOf("'",pwIndex+11));
}
buf.append(argStr);
}
}
log.info("args{}", buf.append("]").toString());
}
}*/
/* @AfterReturning(returning = "object", pointcut = "accessLog()")
public void doAfterReturning(Object object) {
if (null != object) {
log.info("response={}", object.toString());
}
}*/
@Around("accessLog()")
public Object around(ProceedingJoinPoint joinPoint){
long startTime = System.currentTimeMillis();
// 定义返回对象、得到方法需要的参数
Object resultData = null;
Object[] args = joinPoint.getArgs();
String uri = "";
String methodName = joinPoint.getSignature().getName();
try {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
uri = request.getServletPath();
// log.info("ip[{}]url[{}]", request.getRemoteAddr(), request.getRequestURL());
String loginName = HttpUtil.getCookieValue(request, SysConstains.COOKIE_PIN);
// log.info("用户:" + loginName + ";访问接口:" + uri);
log.info("======>用户:{},请求[{}]接口", loginName, request.getServletPath());
if (joinPoint != null && joinPoint.getArgs() != null) {
StringBuffer buf = new StringBuffer("[");
for (int i = 0; i < joinPoint.getArgs().length; i++) {
Object arg = joinPoint.getArgs()[i];
if (arg != null && !(arg instanceof ServletRequest) && !(arg instanceof ServletResponse)) {
String argStr = arg.toString();
//针对登录密码日志,做特殊处理
if (argStr.contains("password") && argStr.contains("LoginForm")){
int pwIndex = argStr.indexOf("password=");
argStr = argStr.substring(0,pwIndex+10)+"****"+argStr.substring(argStr.indexOf("'",pwIndex+11));
}
buf.append(argStr);
}
}
log.info("args{}", buf.append("]").toString());
}
resultData = joinPoint.proceed(args);
long endTime = System.currentTimeMillis();
log.info("======>用户:{},完成请求[{}],耗时:{},返回:{}", loginName, uri, (endTime - startTime), resultData.toString());
} catch (Throwable e) {
e.printStackTrace();
// 记录异常信息
long endTime = System.currentTimeMillis();
log.error("======>请求[{}]异常!耗时:{}", uri, (endTime - startTime));
}
return resultData;
}
}
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)