Java 接口报错日志 aop

接口报错日志

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;


}

二级目录

三级目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值