aop
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.morimatsu.polaris.entity.CallInterfaceLog;
import cn.morimatsu.polaris.service.ICallInterfaceLogService;
import cn.morimatsu.polaris.utils.ExceptionHandlerUtils;
import cn.morimatsu.polaris.utils.IpUtil;
import cn.morimatsu.polaris.utils.UserUtil;
import cn.morimatsu.polaris.vo.UserVo;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
@Aspect
@Component
@SuppressWarnings("unused")
public class SysLogAspect {
@Value("${aop.enable:false}")
boolean enable;
@Value("${aop.save-result:false}")
boolean saveResult;
@Resource
ICallInterfaceLogService logService;
private final Log log = LogFactory.get();
@PostConstruct
public void checkStartupStatus() {
if (enable) log.info("===============AOP已开启===============");
if (saveResult) log.info("===============写入返回结果已开启===============");
}
/**
* 定义切点
*
*/
@Pointcut(value = "execution( * xxx.controller.*.*(..))")
public void aopConfig() {
}
/**
* 切面增强
*
*/
@Around("aopConfig()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
//开关
if (!enable) return joinPoint.proceed();
//记录数据实体类
final CallInterfaceLog data = new CallInterfaceLog();
//设置IP地址
data.setIp(IpUtil.getUserIp4Address());
//开始执行时间戳
long beginTime = DateUtil.current();
//请求的方法名
String className = joinPoint.getTarget().getClass().getName();
String methods = joinPoint.getSignature().getName();
data.setMethod(className.substring(className.lastIndexOf(".") + 1) + "." + methods);
//请求的参数
//设置用户信息 如果为系统则为空
try {
UserVo user = UserUtil.getUser();
data.setUser(Math.toIntExact(user.getId()));
data.setCompany(Math.toIntExact(user.getCompanyId()));
data.setProject(Math.toIntExact(user.getProjectId()));
data.setSubmitParameters(JSON.toJSONString(joinPoint.getArgs()));
} catch (Exception ignored) {
}
//结束返回的参数
Object result;
try {
result = joinPoint.proceed();
if (saveResult) data.setReturnParameters(JSON.toJSONString(result));
} catch (Exception exception) {
//异常后记录状态
data.setStatus(0);
data.setException(JSON.toJSONString(ExceptionHandlerUtils.exceptionMessage(exception, this.getClass())));
//执行时长(毫秒)
data.setElapsedTime(NumberUtil.round(NumberUtil.div(DateUtil.current() - beginTime, 1000), 2));
logService.save(data);
//继续抛出异常 交给全局异常处理
throw exception;
}
//12-7-22 关闭正常执行记录日志
//执行时长(毫秒)
// data.setElapsedTime(NumberUtil.round(NumberUtil.div(DateUtil.current() - beginTime, 1000), 2));
// try {
// logService.save(data);
// } catch (Exception ignored) {
// }
return result;
}
日志实体
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* <p>
* 系统接口调用日志
* </p>
*
* @author ws
* @since 2022-09-14
*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("p_call_interface_log")
@ApiModel(value = "CallInterfaceLog对象", description = "系统接口调用日志")
public class CallInterfaceLog implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ApiModelProperty(value = "被调用的方法")
private String method;
@ApiModelProperty(value = "提交参数")
private String submitParameters;
@ApiModelProperty(value = "返回参宿")
private String returnParameters;
@ApiModelProperty(value = "调用时间")
private LocalDateTime time;
@ApiModelProperty(value = "调用ip")
private String ip;
@ApiModelProperty(value = "调用用户")
private Integer user;
@ApiModelProperty(value = "所属公司")
private Integer company;
@ApiModelProperty(value = "所属项目")
private Integer project;
@ApiModelProperty(value = "调用状态")
private Integer status;
@ApiModelProperty(value = "执行经过的时间")
private BigDecimal elapsedTime;
@ApiModelProperty(value = "异常")
private String exception;
}
二级目录
三级目录