通过SpringAop功能记录后台日志,避免侵入太多的业务代码,附录简单的代码实现:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.fileupload.FileUploadBase;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
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;
import static com.xdja.pcs.common.constant.Const.HttpConstant.REQ_LOG_MESSAGE_ID;
/**
* 日志interceptor
*
* @author xx
* @since 2019/3/29
*/
@Component
@Scope("prototype")
@Aspect
public class WebLogAspect {
/**
* static logger
*/
private static final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping) ||" +
"@annotation(org.springframework.web.bind.annotation.GetMapping) ||" +
"@annotation(org.springframework.web.bind.annotation.PostMapping) ")
public void controllerAspect() {
}
@Before(value = "controllerAspect()")
public void doBefore(JoinPoint joinPoint) {
if (logger.isDebugEnabled()) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//请求日志
String messageId = RandomUtil.getUUID();
String url = request.getRequestURL().toString();
request.setAttribute(REQ_LOG_MESSAGE_ID, messageId);
logger.debug("---------->请求messageId:{}", messageId);
logger.debug("---------->请求地址:{}", url);
logger.debug("---------->请求方式:{}", request.getMethod());
logger.debug("---------->请求方IP:{}", IpUtils.getIpAddr(request));
logger.debug("---------->传入参数:{}", JSONObject.toJSONString(argsFilter(joinPoint.getArgs())));
}
}
@AfterReturning(value = "controllerAspect()", returning = "result")
public void doAfter(Object result) {
if (logger.isDebugEnabled()) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//输出日志
String messageId = (String) request.getAttribute(REQ_LOG_MESSAGE_ID);
logger.debug("---------->返回messageId:{}", messageId);
logger.debug("---------->返回参数:{}", JSON.toJSONString(result));
}
}
/**
* 参数过滤
* 处理 HttpServletRequest、HttpServletResponse
*/
private Object[] argsFilter(Object[] args) {
List<Object> result = new ArrayList<>();
if (args != null && args.length > 0) {
for (Object arg : args) {
if (arg instanceof HttpServletRequest
|| arg instanceof HttpServletResponse
|| arg instanceof FileUploadBase) {
continue;
}
result.add(arg);
}
}
return result.toArray();
}
}