import java.lang.reflect.Method;
import javax.inject.Named;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import com.jcabi.log.Logger;
/**
* @author zhechen Log Aspect for method
*/
@Aspect
@Named
public class MethodLogger {
@Pointcut("execution(* *(..)) && @annotation(com.jcabi.aspects.Loggable)")
public void logMethod() {
}
@Before(value = "logMethod()")
public void startMethodLog(JoinPoint joinPoint) throws Throwable {
final Method method = ((MethodSignature) joinPoint.getSignature())
.getMethod();
final Class clazz = ((MethodSignature) joinPoint.getSignature())
.getDeclaringType();
long start = System.currentTimeMillis();
Logger.debug(clazz, "invoke %s at %s", method, start);
}
@After(value = "logMethod()")
public void endMethodLog(JoinPoint joinPoint) throws Throwable {
final Method method = ((MethodSignature) joinPoint.getSignature())
.getMethod();
final Class clazz = ((MethodSignature) joinPoint.getSignature())
.getDeclaringType();
long end = System.currentTimeMillis();
Logger.debug(clazz, "finish invoke %s at %s", method, end);
}
}
日志相关的处理,在方法层
Controller拦截处理,around:
private enum ReturnType {
JSON_OBJECT, JSON_RESULT, VOID, PATH, DIRECT_STRING,
}
@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object handlerRequestMapping(final ProceedingJoinPoint joinPoint) throws Throwable {
final Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
ReturnType returnType = ReturnType.PATH;
if (void.class.equals(method.getReturnType())) {// 返回值为void
if (method.getAnnotation(ResponseResult.class) != null) {
returnType = ReturnType.JSON_RESULT;
} else {
returnType = ReturnType.VOID;
}
} else if (String.class.equals(method.getReturnType())) {// 返回值为String
if (method.getAnnotation(NotEscape.class) != null) {
returnType = ReturnType.DIRECT_STRING;
} else {
returnType = ReturnType.PATH;
}
} else {
returnType = ReturnType.JSON_OBJECT;
}
try {
final Object result = joinPoint.proceed();
if (result != null || returnType == ReturnType.JSON_RESULT) {
switch (returnType) {
case DIRECT_STRING:
writeDirectToResponse(ResponseHolder.get(), result);
break;
case JSON_OBJECT:
writeJsonEscapeToResponse(ResponseHolder.get(), result);
break;
case PATH:
// 指向JSP路径,直接返回
return result;
case VOID:
// 方法中已经处理了返回,无需再做操作
break;
case JSON_RESULT:
final AjaxResultBean ajaxResultBean = new AjaxResultBean();
ajaxResultBean.setSuccess(true);
ajaxResultBean.setMessage(ErrorUtil.getSuccessMessage());
ajaxResultBean.setKey(getErrorCode(((MethodSignature) joinPoint.getSignature()).getMethod(), ErrorCode.SUCCESS));
writeJsonEscapeToResponse(ResponseHolder.get(), ajaxResultBean);
break;
default:
break;
}
}
} catch (final Throwable e) {
final AjaxResultBean ajaxResultBean = new AjaxResultBean();
ajaxResultBean.setSuccess(false);
if (YtoBusinessException.class.isAssignableFrom(e.getClass())) {
//TODO FIX
// ajaxResultBean.setKey(getErrorCode(((MethodSignature) joinPoint.getSignature()).getMethod(), ((YtoBusinessException) e).getCode()));
//TODO fix
// ajaxResultBean.setMessage(getErrorMessage(((MethodSignature) joinPoint.getSignature()).getMethod(), ((YtoBusinessException) e).getCode()));
} else {
ajaxResultBean.setKey(getErrorCode(((MethodSignature) joinPoint.getSignature()).getMethod(), ErrorCode.DEFAULT_ERROR));
ajaxResultBean.setMessage(getErrorMessage(((MethodSignature) joinPoint.getSignature()).getMethod(), ErrorCode.DEFAULT_ERROR));
}
switch (returnType) {
case DIRECT_STRING:
writeDirectToResponse(ResponseHolder.get(), ajaxResultBean.getMessage());
break;
case JSON_OBJECT:
writeJsonEscapeToResponse(ResponseHolder.get(), ajaxResultBean);
break;
case PATH:
// 指向JSP路径,直接返回
RequestHolder.get().setAttribute("errorBean", ajaxResultBean);
return "error.index";
case VOID:
// 方法中已经处理了返回,无需再做操作
break;
case JSON_RESULT:
writeJsonEscapeToResponse(ResponseHolder.get(), ajaxResultBean);
break;
default:
break;
}
}
return null;
}