实现aop方法
@Slf4j
@Aspect
@Component
public class LogAspect {
@Autowired
private BaseProperties baseProperties;
@Autowired
private LogService logService;
@Pointcut("@annotation(annotation.Log)")
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint point) throws JsonProcessingException {
Object result = null;
long beginTime = System.currentTimeMillis();
try {
result = point.proceed();
} catch (Throwable e) {
log.error(e.getMessage());
}
HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
String ip = IPUtil.getIpAddr(request);
long time = System.currentTimeMillis() - beginTime;
if (baseProperties.isOpenAopLog()) {
String token = (String) SecurityUtils.getSubject().getPrincipal();
String username = JWTUtil.getUsername(token);
SysLog log = new SysLog();
log.setUsername(username);
log.setIp(ip);
log.setTime(time);
logService.saveLog(point, log);
}
return result;
}
}
自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
String value() default "";
}