日志切面注解

这篇博客介绍了如何使用Java注解定义日志统一接口,并通过AOP(面向切面编程)进行方法执行前后的日志记录。日志处理类`LogHandler`作为切面,监听标记了`@LogEvent`注解的方法,根据注解信息插入对应的日志记录到数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/**
 * 日志统一接口
 *
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)  //描述注解的适用场景(即注解的使用地方)
@Documented
public @interface LogEvent {

    /**
     * 事件id(对应LogEventEnum中的id)
     */
    int eventId() default 0;

    /**
     * 事件名(不加默认为LogEventEnum中的name)
     */
    String eventName() default "";

    /**
     * 是否需要成功失败标识(默认不需要)
     */
    boolean eventStatus() default false;
}



@Component // 切记,一定要将切面交由Spring管理,否则不起作用
@Aspect // 申明是一个切面
@Slf4j  // @Slf4j是用作日志输出的,添加以后可以直接调用log.info()
/**
 * 日志接口监控
 *
 */
public class LogHandler {
    @Resource
    private TLogMapper tLogMapper;
    // 使用注解的方法定义切点一般会和自定义注解配合使用 Pointcut 申明的切点
    @Pointcut("@annotation(com.cncr.*.common.handler.LogEvent)")
    public void pointCut() {

    }

    /**
     * 方法切入点(输出方法执行概要日志)
     *
     * @param joinPoint 连接点
     * @return 原方法返回值
     */
    @AfterReturning(value = "pointCut()")
    public void afterReturning(JoinPoint joinPoint) {
        insertLog(joinPoint, (byte) 1);
    }

    @AfterThrowing(value = "pointCut()")
    public void afterThrowing(JoinPoint joinPoint) {

        insertLog(joinPoint, (byte) 0);
    }

//    @Around("pointCut()")
//    public void executionAround(ProceedingJoinPoint pjp) throws Throwable{
//        System.out.println("execution aspect Around(before) invoke!");
//        System.out.println(pjp.proceed());
//
//        System.out.println("execution aspect Around(after) invoke!");
//    }

    public void insertLog(JoinPoint joinPoint, Byte result) {

        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();

        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        LogEvent logEvent = method.getAnnotation(LogEvent.class);

        //事件id
        int eventId = logEvent.eventId();

        if (eventId == 0) {
            return;
        }

        //事件名称
        String eventName = logEvent.eventName();
        if (StringUtils.isEmpty(eventName)) {
            try {
                eventName = Objects.requireNonNull(LogEventEnum.LogEventEnum(eventId)).getName();
            } catch (Exception e) {
                return;
            }
        }

        //是否需要成功与失败标识
        boolean eventStatus = logEvent.eventStatus();
        if (eventStatus) {

            String status = result == (byte) 1 ? "成功" : "失败";
            eventName = eventName + status;
        }

        String userName = LogSetConfig.getUsername(httpServletRequest);

        TLog tLog = new TLog();
        tLog.setGmtCreate(new Date());
        tLog.setGmtModified(new Date());
        tLog.setLogeventId((long) eventId);
        tLog.setLogtext(eventName);
        tLog.setOperateTerminalIp("");
        tLog.setOperateObject("");
        tLog.setOperateUsername(userName);
        tLog.setOperateResult(result);

        tLogMapper.insert(tLog);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值