java 自定义注解储存日志到数据库

新建一个包,创建注释类
在这里插入图片描述

package com.jeeplus.modules.pc.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author Administrator
 */

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface HlwyyLogger {

// category 和value描述方法的实际作用

    String value() default "";
    int category() default 0;

}

现在来写HlwyyLogger 注解的实现类

import com.jeeplus.core.persistence.CustomException;

import com.jeeplus.modules.pc.doctormodel.entity.DtDoctorsofPc;
import com.jeeplus.modules.pc.doctormodel.service.DtDoctorsofPcService;
import com.jeeplus.modules.pc.logrecord.entity.DtLogRecordofPc;
import com.jeeplus.modules.pc.logrecord.service.DtLogRecordofPcService;
import com.jeeplus.modules.pc.tools.HttpContextUtils;
import com.jeeplus.modules.pc.tools.IpUtilsofDiy;
import com.jeeplus.modules.pc.tools.JSONUtils;
import com.jeeplus.modules.sys.security.util.JWTUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author Administrator
 */
@Aspect
@Component
public class HlwyyLoggerImpl {


           //本地异常日志记录对象 
        private final static Logger logger = LoggerFactory.getLogger(HlwyyLogger.class);
        
        @Autowired(required = false)
         private DtDoctorsofPcService dtDoctorsService;
         
      //注入Service用于把日志保存数据库 
        @Autowired(required = false)
        private DtLogRecordofPcService dtLogRecordofPcService;

    //Controller层切点
    @Pointcut("@annotation(com.jeeplus.modules.pc.config.HlwyyLogger)")
    public void HlwyyLogger() {
    }

    @Before("HlwyyLogger()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {

    }

     //joinPoint 切点 

    @Around("HlwyyLogger()")
    public Object around(ProceedingJoinPoint point) throws CustomException {
        // 执行方法
        Object result = null;
        this.checkPermission(point);
        try {
            // 执行方法
            result = point.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return result;
    }

    void checkPermission(ProceedingJoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        HlwyyLogger syslog = method.getAnnotation(HlwyyLogger.class);
        Boolean b = false;
        if (syslog != null) {
            try {
                String params = JSONUtils.beanToJson(syslog.value());
                String params1 = JSONUtils.beanToJson(syslog.category());
                logger.info("参数为:" + params);
                logger.info("参数为:" + params1);

                // 获取request(没有token可以不用写)
                HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
                DtLogRecordofPc dtLogRecordofPc=new DtLogRecordofPc();
                try{
                    String username= JWTUtil.getLoginName(request.getHeader("token"));
                    if(!StringUtils.isBlank(username))
                    {
                        DtDoctorsofPc doctor=this.dtDoctorsService.findUniqueByProperty("doctor_name",username);
                        if(doctor!=null)
                        {
                            dtLogRecordofPc.setDoctorId(Integer.parseInt(doctor.getId()));
                        }
                    }
                }catch (Exception e)
                {

                }
                //需要被存入数据库的字段
                dtLogRecordofPc.setCreateTime(new Date());
                dtLogRecordofPc.setStatus(1);
                dtLogRecordofPc.setContent(params+" url: "+request.getRequestURI());
                dtLogRecordofPc.setCategory(syslog.category());
                dtLogRecordofPc.setType(2);
                dtLogRecordofPc.setUserAgent(request.getHeader("user-agent"));
                dtLogRecordofPc.setIp(IpUtilsofDiy.getIpAddr(request));
                this.dtLogRecordofPcService.save(dtLogRecordofPc);
            } catch (Exception e) {
                logger.debug(e.getMessage());
            }
        }

    }
}

注解的实现类(也可以说是切点类)写完,我们就可以对自己的接口添加注解从而将你的操作步骤存入数据库

在这里插入图片描述
在这里插入图片描述

红色框住的就是我们需要添加的注解
由于我是两个端的日志写在一起所以需要 “category”这个字段来区分,1是后台端,2是用户端。不需要区分的同学只写上注解和你的接口说明(value)就可以啦
在这里插入图片描述

感谢观看!!!
下次再见!!!
我是一条小咸鱼

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值