Spring 使用注解集成Log

来源:互联网 发布:淘宝血滴子 编辑:程序博客网 时间:2024/06/05 18:12
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface WesLogable {}



@Component@SuppressWarnings("serial")public class WesLogAdvisor extends AbstractPointcutAdvisor {private final StaticMethodMatcherPointcut pointcut = new StaticMethodMatcherPointcut() {@Overridepublic boolean matches(Method method, Class<?> targetClass) {return method.isAnnotationPresent(WesLogable.class);}};@Autowiredprivate WesLogInterceptor interceptor;@Overridepublic Pointcut getPointcut() {return this.pointcut;}@Overridepublic Advice getAdvice() {return this.interceptor;}}
@Componentpublic class WesLogInterceptor implements MethodInterceptor,Ordered {private Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());@Autowiredprivate LogRepository logRepository;@Overridepublic 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());}}@Overridepublic int getOrder() {return 1;}}

测试:

@WesLogable

public void testLog(String name, int age){

}

0 0
原创粉丝点击