@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){
}