Spring 使用注解集成Log

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface WesLogable {

}



@Component
@SuppressWarnings("serial")
public class WesLogAdvisor extends AbstractPointcutAdvisor {

	private final StaticMethodMatcherPointcut pointcut = new StaticMethodMatcherPointcut() {
		@Override
		public boolean matches(Method method, Class<?> targetClass) {
			return method.isAnnotationPresent(WesLogable.class);
		}
	};

	@Autowired
	private WesLogInterceptor interceptor;

	@Override
	public Pointcut getPointcut() {
		return this.pointcut;
	}

	@Override
	public Advice getAdvice() {
		return this.interceptor;
	}

}
@Component
public class WesLogInterceptor implements MethodInterceptor,Ordered {
	private Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());

	@Autowired
	private LogRepository logRepository;

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		long start = System.currentTimeMillis();
		String message = null;
		Timestamp startDate = new Timestamp(start);

		try {
			
			return invocation.proceed();
		} catch (WesException e) {
			message = e.getErrorMessage();
			throw e;
		}catch(Exception e){
			message = e.getMessage();
			throw e;
		} finally {
			long end = System.currentTimeMillis();
			after(invocation, end - start, message, startDate);
		}
	}

	private void after(MethodInvocation invocation, long duration, String message, Timestamp startDate) {
		Log log = prepareLogData(invocation, duration, message, startDate);
		persistLog(log);
	}

	private void persistLog(Log log) {
		logRepository.save(log);
	}

	private Log prepareLogData(MethodInvocation invocation, long duration, String message, Timestamp startDate) {
		Object[] arguments = getArgumentArray(invocation);
		Parameter[] parameters = invocation.getMethod().getParameters();

		String url = getUri(invocation);
		String urlData = null;
		String bodyData = null;
		String type = "in";

		HashMap<String, String> map = new HashMap<>();

		for (int i = 0; i < parameters.length; i++) {
			Parameter param = parameters[i];

			if (param.isAnnotationPresent(RequestBody.class)) {
				bodyData = arguments[i].toString();
				continue;
			} else {
				if(null != param && null != arguments[i]){
					map.put(param.getName(), arguments[i].toString());
				}
			}
		}

		urlData = JsonUtils.object2JsonStr(map);

		Log log = new Log();

		log.setDuration(duration);
		log.setMessage(message);
		log.setStartDate(startDate);
		log.setUrl(url);
		log.setType(type);
		log.setBodyData(bodyData);
		log.setUrlData(urlData);

		return log;
	}

	private String getUri(MethodInvocation invocation) {
		RequestMapping rm = invocation.getMethod().getAnnotation(RequestMapping.class);
		RequestMapping classRm = invocation.getThis().getClass().getAnnotation(RequestMapping.class);
		
		
		if (null != rm) {
			String url = rm.value()[0];
			
			if (null != classRm) {
				String classUrl = classRm.value()[0];
				url = classUrl + url;
			}
			
			return url;
		}
		
		return null;
	}

	private Object[] getArgumentArray(MethodInvocation invocation) {
		Object[] args = new Object[invocation.getArguments().length];
		for (int i = 0; i < args.length; i++) {
			args[i] = invocation.getArguments()[i];
			printArgumentValue(args[i]);
		}
		return args;
	}

	private void printArgumentValue(Object obj) {
		if (obj == null) {
			logger.info("    >>> Null");
		} else {
			logger.info("    >>> {}", obj.toString());
		}
	}

	@Override
	public int getOrder() {
		return 1;
	}

}

测试:

@WesLogable

public void testLog(String name, int age){

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值