com.healthy.common.log.aspect.LogAspect Maven / Gradle / Ivy
The newest version!
package com.healthy.common.log.aspect;
import cn.hutool.json.JSONUtil;
import com.healthy.common.core.util.SpringContextHolder;
import com.healthy.common.log.annotation.Log;
import com.healthy.common.log.enums.BusinessStatus;
import com.healthy.common.log.event.LogInfoEvent;
import com.healthy.common.log.util.LogInfo;
import com.healthy.common.log.util.LogUtils;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import java.util.Objects;
/**
* 操作日志使用spring event异步入库
*
* @author xiaomingzhang
*/
@Aspect
public class LogAspect {
@Around("@annotation(log)")
@SneakyThrows
public Object around(ProceedingJoinPoint point, Log log) {
LogInfo logInfo = LogUtils.getLogInfo(point, log);
logInfo.setTitle(log.title());
// 发送异步日志事件
Long startTime = System.currentTimeMillis();
Object obj = null;
try {
obj = point.proceed();
}
catch (Exception e) {
logInfo.setStatus(BusinessStatus.FAIL.ordinal());
logInfo.setException(e.getMessage());
throw e;
}
finally {
Long endTime = System.currentTimeMillis();
logInfo.setTime(endTime - startTime);
// 返回参数
if (Objects.nonNull(obj)) {
logInfo.setJsonResult(JSONUtil.toJsonStr(obj));
}
SpringContextHolder.publishEvent(new LogInfoEvent(logInfo));
}
return obj;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy