SpringAop系统日志

通过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();
	}

}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值