自定义注解实现日志打印

通常为了监控系统,我们希望将请求的入参和出参记录到数据库中,已备后查。除了在每个方法里面加日志处理代码,手动保存到数据库,还有其他的办法吗?

今天就给大伙介绍一个注解 @Log2DB

什么?没这个注解?是的,这个注解是自定义的,那用这个注解能干什么呢?怎么写一个自己的注解呢?今天我们一起来探讨下。

首先,我们随便找个注解,看下它的结构@RestController

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    @AliasFor(
        annotation = Controller.class
    )
    String value() default "";
}

是个interface,但是又加了一个@符号,那好吧,我们也搞一个这个。

@Target({ElementType.METHOD, ElementType.TYPE})//注解作用的位置,ElementType.METHOD表示该注解仅能作用于方法上
@Retention(RetentionPolicy.RUNTIME)//注解的生命周期,表示注解会被保留到什么阶段,可以选择编译阶段SOURCE、类加载阶段CLASS,或运行阶段RUNTIME
@Documented//注解信息会被添加到Java文档中
public @interface Log2DB {
    String name() default "接口";
    boolean enabled() default true;//是否启用,默认是
}

注解是加进去了,怎么用呢?

@Api("testController相关操作")
@Slf4j
@RestController
@RequestMapping("test")
public class TestController {

    @Log2DB(name = "获取用户")
    @GetMapping("get")
    @ApiOperation("获取用户1")
    public String getUser(@RequestParam("userId")String userId){
        log.info("get is called");
        return "get is called";
    }

    @Log2DB(name = "获取用户")
    @PostMapping("post")
    @ApiOperation("保存用户1")
    public String postUser(@RequestBody Map request){
        log.info("post is called");
        return "post is called";
    }
}

这就加上去了,但不是说好了保存到数据库吗?我们用面向切面把@Log2DB这个注解拦截下来,定义一个aspect

@Aspect
@Configuration
@Slf4j
public class Log2DBAspect {
   //拦截哪个包下的类
   //拦截哪个注解com.example.lesson20.annotation.Log2DB
   @Pointcut("execution(public * *(..)) && @annotation(com.example.lesson20.annotation.Log2DB)")
   public void logs(){
      log.info("@Pointcut");
   }
   
   @Around("logs()")
   public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable {
      log.info("@Around");
      MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Log2DB logAnnotation = signature.getMethod().getAnnotation(Log2DB.class);
        //如果不需要记录到数据库,继续业务调用并返回
        if(!logAnnotation.enabled()){
           return joinPoint.proceed();
        }

        //定义需要从request和response中获取的信息 begin
      String name = "",//请求的服务名称
            url="",//请求服务地址
            method="",//请求方法 get post put
            clientIp="",//客户端ip
            browser="",//浏览器
            clazz_method="",//请求的类和方法名
            headers="", //head头信息
            request="";//请求body入参
      Object response=null;//返回参数
      Date bgn,end;//请求开始结束时间
      Boolean success = true;//是否成功
      long cost = 0;//耗时 毫秒
      //定义需要从request和response中获取的信息 end

      ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
      if(attributes==null){
         return joinPoint.proceed();
      }
      HttpServletRequest httpServletRequest = attributes.getRequest();

      url = httpServletRequest.getRequestURL().toString();
      method = httpServletRequest.getMethod();
      clientIp =getClientIp(httpServletRequest);
      clazz_method =joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName();

      name = logAnnotation.name();

      //获取url后面的参数串
      StringBuffer params = new StringBuffer();
      for (String item : httpServletRequest.getParameterMap().keySet()) {
         params.append(item).append("=").append(httpServletRequest.getParameter(item)).append("&");
      }
      String tmp = params.toString();
      if(StringUtils.hasText(tmp)){
         tmp = tmp.substring(0,tmp.length()-1);
         url = url+"?"+tmp;
      }

      //获取header头信息
      List<Map<String,String>> headerList = new ArrayList<>();
      Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
      while (headerNames.hasMoreElements()) {
         String key = headerNames.nextElement();
         String value = httpServletRequest.getHeader(key);
         Map<String,String> map = new HashMap<String,String>();
         map.put(key,value);
         headerList.add(map);

         if("user-agent".equals(key.toLowerCase())){
            browser = value;
         }
      }
      headers = new ObjectMapper().writeValueAsString(headerList);

      request = new ObjectMapper().writeValueAsString(joinPoint.getArgs());

      //记录起始时间
      bgn = new Date();
      /** 执行目标方法 */
      try{
         response= joinPoint.proceed();
         log.info("response={}",response==null?"": new ObjectMapper().writeValueAsString(response));
      }
      catch(Exception e){
         success=false;
         response = e.getMessage();
         log.error("errorMessage: {}", e.getMessage());
         e.printStackTrace();
         throw e;
      }
      finally{
         end = new Date();
         /** 记录操作时间 */
         cost = (end.getTime() - bgn.getTime());

         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
         log.info("name={}",name);
         log.info("url={}",url);
         log.info("method={}",method);
         log.info("ip={}",clientIp);
         log.info("browser={}",browser);
         log.info("class_method={}",clazz_method);
         log.info("header={}",headers);
         log.info("request={}",request);
         log.info("response={}",response);
         log.info("success={}",success);
         log.info("bgn={}",format.format(bgn));
         log.info("end={}",format.format(end));
         log.info("cost={}", cost);

         //TODO: 保存到数据库 这里大家伙自行sql保存到数据库,我就不参合了
         //不过我建议这个地方可以考虑先放到mq 消息队列中,然后慢慢消费,不要占用mysql的资源。
         // 或者日志就存储到mongodb中也不失为一个好办法
      }
      return response;
   }

   @Before("logs()")
   public void doBefore(JoinPoint point){
      log.info("@Before");
   }

   @After("logs()")
   public void doAfter(){
      log.info("@After");
   }

   //在doAfter之后执行,主要用户记录程序执行后的返回值
   @AfterReturning(returning="object",pointcut="logs()")
   public void doAfterReturning(Object object){
      log.info("@AfterReturning");
   }

   //获取客户端ip
   private String getClientIp(HttpServletRequest request) {
      String ip = request.getHeader("x-forwarded-for");
      if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
         ip = request.getHeader("Proxy-Client-IP");
      }
      if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
         ip = request.getHeader("WL-Proxy-Client-IP");
      }
      if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
         ip = request.getRemoteAddr();
      }
      return ip;
   }
}

类有点长,我就不给大家一一解释了,里面注释都很详细,大家直接copy paste就可以用了,最后我们来验证下是不是这些参数都拿到了,postman请求一次,看看后台打印日志

@Around
@Before
post is called
@AfterReturning
@After
response="post is called"
name=保存用户1
url=http://localhost:8080/test/post?userId=abc
method=POST
ip=0:0:0:0:0:0:0:1
browser=PostmanRuntime/7.26.8
class_method=com.example.lesson20.controller.TestController.postUser
header=[{"content-type":"application/json"},{"user-agent":"PostmanRuntime/7.26.8"},{"accept":"*/*"},{"cache-control":"no-cache"},{"postman-token":"b3f49862-8b4e-4c7a-b817-3ff4f4e19336"},{"host":"localhost:8080"},{"accept-encoding":"gzip, deflate, br"},{"connection":"keep-alive"},{"content-length":"47"}]
request=[{"username":"abc","password":123}]
response=post is called
success=true
bgn=2021-07-03 10:00:25:183
end=2021-07-03 10:00:25:214
cost=31

嗯,是不错。但是方法的中文名字在Log2DBApiOperation中都要写一次,有点麻烦。下一期我们一起来解决这个问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以通过自定义注解实现AOP,其中Spring Boot框架提供了一种简单的方式来创建自定义注解实现AOP。 首先,我们需要定义一个注解,注解可以使用在类、方法或者字段上。我们可以使用元注解`@Target`来指定注解的使用位置,比如在方法上使用。然后,我们可以使用元注解`@Retention`来指定注解的生命周期,比如运行时生命周期。接着,我们可以使用元注解`@Documented`来指定注解是否会保存在JavaDoc文档中。最后,我们还可以使用元注解`@Inherited`来指定注解是否具有可继承性。 在实现AOP时,我们可以通过自定义注解和切面来实现一些增强功能,比如日志打印、方法耗时统计、多数据源切换等。我们可以在自定义注解上添加一些切面逻辑,然后在需要应用这些增强功能的地方使用这个注解。 具体实现过程如下: 1. 定义一个自定义注解,使用`@Target`指定注解的使用位置,使用`@Retention`指定注解的生命周期,使用`@Documented`指定是否保存在JavaDoc文档中,使用`@Inherited`指定是否具有可继承性。 2. 创建一个切面类,使用`@Aspect`注解标识该类为切面类,并在该类中定义一些增强功能的逻辑,比如在方法执行前后打印日志。 3. 在需要应用增强功能的地方,使用自定义注解来标识,例如在方法上添加自定义注解。 4. 在Spring Boot配置类中,通过`@EnableAspectJAutoProxy`注解开启AOP功能。 通过以上步骤,我们就可以在Java中使用自定义注解实现AOP了。这样,我们可以通过在需要应用增强功能的地方使用自定义注解来触发切面逻辑,从而实现AOP的效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Java AOP自定义注解](https://blog.csdn.net/baidu_28340727/article/details/128319277)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值