使用spring boot通過(guò)自定義注解打印所需日志
spring boot自定義注解打印日志
在實(shí)際項(xiàng)目中可能需要監(jiān)控每個(gè)接口的請(qǐng)求時(shí)間以及請(qǐng)求參數(shù)等相關(guān)信息,那么此時(shí)我們想到的就是兩種實(shí)現(xiàn)方式,一種是通過(guò)攔截器實(shí)現(xiàn),另一種則通過(guò)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注解用來(lái)定義切面的執(zhí)行順序,數(shù)值越小優(yōu)先級(jí)越高。
2.@Around環(huán)繞通知,我們可以自定義在什么時(shí)候執(zhí)行@Before以及@After。
3.ThreadLocal針對(duì)每個(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("請(qǐng)求URL:"+request.getRequestURL());
System.out.println("請(qǐng)求參數(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)繞方法開(kāi)始************");
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;
}
}
測(cè)試結(jié)果

通過(guò)自定義注解獲取日志
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è)測(cè)試用類,并使用注解
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("永無(wú)bug");
}
}
3.使用注解,在測(cè)試類中輸出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)文章
SpringBoot實(shí)現(xiàn)異步調(diào)用的方法示例
本文介紹了在Java的SpringBoot中實(shí)現(xiàn)異步請(qǐng)求和異步調(diào)用的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
Java8 Supplier接口和Consumer接口原理解析
這篇文章主要介紹了Java8 Supplier接口和Consumer接口原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
java正則表達(dá)式之Pattern與Matcher類詳解
這篇文章主要給大家介紹了關(guān)于java正則表達(dá)式之Pattern與Matcher類的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
SpringMVC @RequestBody出現(xiàn)400 Bad Request的解決
這篇文章主要介紹了SpringMVC @RequestBody出現(xiàn)400 Bad Request的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Java將中文轉(zhuǎn)化為拼音的簡(jiǎn)單代碼示例
在我們使用手機(jī)通訊錄或各種APP的搜索功能時(shí),既可以根據(jù)中文搜索,也可以根據(jù)拼音搜索,這種時(shí)候就使用到了中文轉(zhuǎn)拼音的功能了,下面這篇文章主要給大家介紹了關(guān)于Java將中文轉(zhuǎn)化為拼音的簡(jiǎn)單代碼示例,需要的朋友可以參考下2024-03-03
SpringBoot 使用Prometheus采集自定義指標(biāo)數(shù)據(jù)的方案
這篇文章主要介紹了SpringBoot 使用Prometheus采集自定義指標(biāo)數(shù)據(jù),我們?cè)趉8s集群成功搭建了Prometheus服務(wù),今天,我們將在springboot2.x中使用prometheus記錄指標(biāo),需要的朋友可以參考下2022-10-10
springboot下添加日志模塊和設(shè)置日志文件輸出的方法
日志的使用將通過(guò)SLF4J來(lái)使用,SLF4J是一個(gè)為Java應(yīng)用提供簡(jiǎn)單日志記錄的接口,在Spring框架中,SLF4J常常用于處理框架本身以及應(yīng)用程序的日志記錄,本文給大家介紹springboot下添加日志模塊和設(shè)置日志文件輸出的相關(guān)知識(shí),感興趣的朋友一起看看吧2023-12-12

