package com.xcong.excoin.common.aop; import com.xcong.excoin.common.system.bean.SysExceptionDetailEntity; import com.xcong.excoin.modules.platform.dao.SysExceptionDetailDao; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.PrintWriter; import java.io.StringWriter; import java.net.InetAddress; import java.util.Date; /** * @author wzy * @date 2021-03-05 **/ @Slf4j @Aspect @Component public class ExceptionCatchAspect { @Autowired private SysExceptionDetailDao sysExceptionDetailDao; @Pointcut("execution(* com.xcong.excoin..*.*(..))") public void exceptionCatch() { } @AfterThrowing(pointcut = "exceptionCatch()", throwing = "ex") public void afterThrows(JoinPoint jp, Exception ex) throws Exception { SysExceptionDetailEntity exceptionData = new SysExceptionDetailEntity(); exceptionData.setCreateTime(new Date()); exceptionData.setMachine(InetAddress.getLocalHost().getHostName()); exceptionData.setAddress(InetAddress.getLocalHost().getHostAddress()); exceptionData.setExceptionMsg(printStackTraceToString(ex.fillInStackTrace())); exceptionData.setSimpleMsg(ex.getMessage()); sysExceptionDetailDao.insert(exceptionData); throw ex; } public String printStackTraceToString(Throwable t) { StringWriter sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw, true)); return sw.getBuffer().toString(); } }