切面日志
@Component //聲明元件
@Aspect // 聲明切面
@ComponentScan //元件自動掃描
@EnableAspectJAutoProxy //spring自動切換JDK動态代理和CGLIB
public class LogAspect {
/**
* 自定義日志
*/
private Logger logger = LoggerFactory.getLogger(LogAspect.class);
/**
* 在方法執行前進行切面 controller層下的所有方法
*/
@Pointcut("execution(public * com.mtcarpenter.mall.*.controller..*(..)) || execution(public * com.mtcarpenter.*.controller..*(..))")
public void log() {
}
@AfterReturning(value = "log()", returning = "returnValue")
public void before(JoinPoint point, Object returnValue) {
logger.info("-------------------------請求開始-------------------------");
logger.info("請求類方法:" + point.getSignature());
String[] paramNames = ((CodeSignature) point.getSignature()).getParameterNames();
Object[] paramValues = point.getArgs();
for (int i = 0; i < paramNames.length; i++) {
logger.info("請求類方法參數:" + paramNames[i] + ":" + paramValues[i]);
}
//傳回值類型
//System.out.println("傳回值:" + ((CommonResult) returnValue).getData());
if (((CommonResult) returnValue).getData() != null) {
String typeName = ((CommonResult) returnValue).getData().getClass().getTypeName();
if (typeName.equals(CommonPage.class.getTypeName())) {
logger.info("請求類方法傳回值:" + ((CommonResult<CommonPage>) returnValue).getData().getList());
} else {
logger.info("請求類方法傳回值:" + ((CommonResult) returnValue).getData());
}
}
logger.info("-------------------------請求結束-------------------------");
}
}