| | |
| | | import java.lang.reflect.Method; |
| | | import java.util.TimerTask; |
| | | |
| | | /** |
| | | * 日志切面类 |
| | | * @author WWW |
| | | */ |
| | | @Aspect |
| | | @Component |
| | | @SuppressWarnings("ALL") |
| | | public class LogAspect { |
| | | @Autowired |
| | | private TokenService tokenService; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 环绕通知 @Around,也可以使用 @Before (前置通知) 或 @After (后置通知) |
| | | * @Around,@Before,@After |
| | | */ |
| | | @Around("logPointCut()") |
| | | public Object around(ProceedingJoinPoint point) throws Throwable { |
| | | long beginTime = System.currentTimeMillis(); |
| | | |
| | | // 执行方法 |
| | | Object result = point.proceed(); |
| | | |
| | | // 执行时长(毫秒) |
| | | long time = System.currentTimeMillis() - beginTime; |
| | | |
| | | // 保存日志 |
| | | saveLog(point, time); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 保存日志 |
| | | */ |
| | | private void saveLog(ProceedingJoinPoint joinPoint, long time) { |
| | | try { |
| | | MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
| | |
| | | oe.setUrl(url); |
| | | oe.setExec(time); |
| | | |
| | | // 设置模块 |
| | | Api api = clazz.getAnnotation(Api.class); |
| | | if (api != null) { |
| | | String[] strs = api.tags()[0].split(StaticData.BACKSLASH); |
| | |
| | | oe.setModular2(strs[1]); |
| | | } |
| | | |
| | | // 设置类名\方法名、类别 |
| | | oe.setClazz(clazz.getName() + "." + method.getName()); |
| | | oe.setType(getType(method.getName())); |
| | | |
| | | // 设置备注 |
| | | SysLog sysLog = method.getAnnotation(SysLog.class); |
| | | if (sysLog != null) { |
| | | oe.setBak(sysLog.value()); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取操作类型 |
| | | */ |
| | | private static int getType(String methodName) { |
| | | if (methodName.contains(SELECT)) { |
| | | return 1; |