亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用spring boot通過自定義注解打印所需日志

 更新時(shí)間:2021年07月26日 15:00:24   作者:kin_wen  
這篇文章主要介紹了使用spring boot通過自定義注解打印所需日志的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

spring boot自定義注解打印日志

在實(shí)際項(xiàng)目中可能需要監(jiān)控每個(gè)接口的請求時(shí)間以及請求參數(shù)等相關(guān)信息,那么此時(shí)我們想到的就是兩種實(shí)現(xiàn)方式,一種是通過攔截器實(shí)現(xiàn),另一種則通過AOP自定義注解實(shí)現(xiàn)。

本文介紹自定義注解實(shí)現(xiàn)方式

自定義注解,四個(gè)元注解這次就不解釋了。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface WebLog {
    /**
     * 日志信息描述
     */
    String description() default "";
}

AOP實(shí)現(xiàn):

1.@Order注解用來定義切面的執(zhí)行順序,數(shù)值越小優(yōu)先級越高。

2.@Around環(huán)繞通知,我們可以自定義在什么時(shí)候執(zhí)行@Before以及@After。

3.ThreadLocal針對每個(gè)線程都單獨(dú)的記錄。

@Aspect
@Component
public class WebLogAspect {
    private static ThreadLocal<ProceedingJoinPoint> td = new ThreadLocal<>();
    @Pointcut("@annotation(com.example.demo.annotation.WebLog)")
    @Order(1)
    public void webLog(){}
    @Before("webLog()")
    public void doBefor(JoinPoint point){
        System.out.println("***********method before執(zhí)行************");
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        System.out.println("請求URL:"+request.getRequestURL());
        System.out.println("請求參數(shù):"+ Arrays.toString(point.getArgs()));
        System.out.println("***********method before結(jié)束************");
    }
    @Around("webLog()")
    public Object doAround(ProceedingJoinPoint point) throws Throwable {
        System.out.println("***********執(zhí)行環(huán)繞方法開始************");
        td.set(point);
        long startTime = System.currentTimeMillis();
        ProceedingJoinPoint joinPoint = td.get();
        Object proceed = joinPoint.proceed();
        System.out.println("執(zhí)行耗時(shí)毫秒:"+ (System.currentTimeMillis()-startTime));
        System.out.println("***********執(zhí)行環(huán)繞方法結(jié)束************");
        return proceed;
    }
}

Controller

@RestController
public class LoginController {
    @PostMapping("/user/login")
    @WebLog(description = "用戶登錄接口")
    public UserForm login(@RequestBody UserForm user){
        return user;
    }
}

測試結(jié)果

在這里插入圖片描述

通過自定義注解獲取日志

1.定義一個(gè)注解

package com.hisense.demo02; 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
/**
 * @author : sunkepeng  E-mail : sunkepengouc@163.com
 * @date : 2020/8/8 20:09
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Check {
}

2.寫一個(gè)測試用類,并使用注解

package com.hisense.demo02;
 
/**
 * @author : sunkepeng  E-mail : sunkepengouc@163.com
 * @date : 2020/8/8 20:04
 */
public class Calculator {
    @Check
    public void add(){
        System.out.println("1+0=" + (1+0));
    }
    @Check
    public void sub(){
        System.out.println("1-0=" + (1-0));
    }
    @Check
    public void mul(){
        System.out.println("1*0=" + (1*0));
    }
    @Check
    public void div(){
        System.out.println("1/0=" + (1/0));
    }
    public void show(){
        System.out.println("永無bug");
    }
}

3.使用注解,在測試類中輸出log

package com.hisense.demo02; 
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
 
/**
 * @author : sunkepeng  E-mail : sunkepengouc@163.com
 * @date : 2020/8/8 21:39
 */
public class TestCheck {
    public static void main(String[] args) throws IOException {
        Calculator calculator = new Calculator();
        Class calculatorClass = calculator.getClass();
        Method[] methods = calculatorClass.getMethods();
        int number =0;
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("bug.txt"));
        for (Method method : methods) {
            if (method.isAnnotationPresent(Check.class)){
                try {
                    method.invoke(calculator);
                } catch (Exception e) {
                    number++;
                    bufferedWriter.write(method.getName()+"出現(xiàn)異常");
                    bufferedWriter.newLine();
                    bufferedWriter.write("異常的名稱:"+e.getCause().getClass().getSimpleName());
                    bufferedWriter.newLine();
                    bufferedWriter.write("異常的原因"+e.getCause().getMessage());
                    bufferedWriter.newLine();
                    bufferedWriter.write("-----------------");
                    bufferedWriter.newLine();
                }
            }
        }
        bufferedWriter.write("本次共出現(xiàn):"+number+"次異常");
        bufferedWriter.flush();
        bufferedWriter.close();
    }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Mybatis中3種關(guān)聯(lián)關(guān)系的實(shí)現(xiàn)方法示例

    Mybatis中3種關(guān)聯(lián)關(guān)系的實(shí)現(xiàn)方法示例

    這篇文章主要給大家介紹了關(guān)于Mybatis中3種關(guān)聯(lián)關(guān)系的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • jeefast和Mybatis實(shí)現(xiàn)三級聯(lián)動(dòng)的示例代碼

    jeefast和Mybatis實(shí)現(xiàn)三級聯(lián)動(dòng)的示例代碼

    這篇文章主要介紹了jeefast和Mybatis實(shí)現(xiàn)三級聯(lián)動(dòng)的示例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 解決mybatis批量更新出現(xiàn)SQL報(bào)錯(cuò)問題

    解決mybatis批量更新出現(xiàn)SQL報(bào)錯(cuò)問題

    這篇文章主要介紹了mybatis批量更新出現(xiàn)SQL報(bào)錯(cuò),解決辦法也很簡單只需要在application.properties配置文中的數(shù)據(jù)源url后面添加一個(gè)參數(shù),需要的朋友可以參考下
    2022-02-02
  • mybatis?resultMap之collection聚集兩種實(shí)現(xiàn)方式

    mybatis?resultMap之collection聚集兩種實(shí)現(xiàn)方式

    本文主要介紹了mybatis?resultMap之collection聚集兩種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-09-09
  • 基于Listener監(jiān)聽器生命周期(詳解)

    基于Listener監(jiān)聽器生命周期(詳解)

    下面小編就為大家?guī)硪黄贚istener監(jiān)聽器生命周期(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • Spring超詳細(xì)講解創(chuàng)建BeanDefinition流程

    Spring超詳細(xì)講解創(chuàng)建BeanDefinition流程

    Spring在初始化過程中,將xml中定義的對象解析到了BeanDefinition對象中,我們有必要了解一下BeanDefinition的內(nèi)部結(jié)構(gòu),有助于我們理解Spring的初始化流程
    2022-06-06
  • java實(shí)現(xiàn)時(shí)鐘效果

    java實(shí)現(xiàn)時(shí)鐘效果

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 最新評論