如何自定義springboot-starter日志組件供各個(gè)服務(wù)使用(系統(tǒng)日志優(yōu)化)
在優(yōu)化項(xiàng)目時(shí)發(fā)現(xiàn)各個(gè)微服務(wù)都有各自的接口調(diào)用日志邏輯,比如每個(gè)服務(wù)都定義一個(gè)aop類攔截,十分冗余,其實(shí)是可以做成starter被各個(gè)服務(wù)引用使用,前提要先了解一下springboot自動(dòng)裝配原理
創(chuàng)建springboot工程,如果是jdk8,下面的地址換成阿里的才能選
pom依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ming</groupId> <artifactId>log</artifactId> <version>0.0.1-SNAPSHOT</version> <name>customlog-spring-boot-starter</name> <description>customlog-spring-boot-starter</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.6.13</spring-boot.version> <lombok.version>1.18.26</lombok.version> </properties> <dependencies> <!--aop--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!--寫application.yml時(shí)會(huì)有提示,即當(dāng)我輸入custom時(shí),會(huì)提示custom.log.serviceName--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
項(xiàng)目結(jié)構(gòu)很簡(jiǎn)單
注解
package com.ming.log.module.annotation; import java.lang.annotation.*; /** * 日志注解 * @author ming */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Documented public @interface CustomLogAnnotation { }
切面邏輯
package com.ming.log.module.aop; import com.ming.log.module.config.CustomLogProperties; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import java.lang.reflect.Method; import java.util.Arrays; /** * aop統(tǒng)一攔截 * @author ming */ @Slf4j @Aspect public class CustomLogAspect { private CustomLogProperties customLogProperties; /** * 當(dāng)一個(gè)bean沒有無(wú)參構(gòu)造器時(shí),spring創(chuàng)建bean時(shí),對(duì)于構(gòu)造器參數(shù)會(huì)從容器中取, * 這里其實(shí)是省略了@Autowired,該注解可以用在方法參數(shù)上 * @param customLogProperties */ public CustomLogAspect(CustomLogProperties customLogProperties) { this.customLogProperties = customLogProperties; } @Around("@annotation(com.ming.log.module.annotation.CustomLogAnnotation)") public Object logInvoke(ProceedingJoinPoint joinPoint) throws Throwable { String serviceName = customLogProperties.getServiceName(); //獲取方法名稱 Signature sig = joinPoint.getSignature(); MethodSignature mSig = (MethodSignature)sig; Method method = mSig.getMethod(); String methodName = method.getName(); Object obj = joinPoint.proceed(); log.error("服務(wù)名:{}",serviceName); log.error("方法名:{}",methodName); log.error("方法參數(shù):{}",Arrays.toString(joinPoint.getArgs())); log.error("方法返回值:{}",obj.toString()); return obj; } }
熟悉配置類
package com.ming.log.module.config; import org.springframework.boot.context.properties.ConfigurationProperties; import javax.annotation.PostConstruct; //讓配置文件中的屬性根據(jù)前綴來(lái)注入對(duì)應(yīng)的屬性 @ConfigurationProperties(value ="custom.log") public class CustomLogProperties { //日志服務(wù)名,會(huì)自動(dòng)找配置文件中customLog.serviceName private String serviceName; public String getServiceName() { return serviceName; } public void setServiceName(String serviceName) { this.serviceName = serviceName; } }
啟用配置
package com.ming.log.module.config; import com.ming.log.module.aop.CustomLogAspect; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration //將CustomLogProperties注入Spring容器 @EnableConfigurationProperties(CustomLogProperties.class) public class CustomLogAutoConfiguration { @Bean public CustomLogAspect customLogAspect(CustomLogProperties customLogProperties){ return new CustomLogAspect(customLogProperties); } }
在resources下創(chuàng)建一個(gè)META-INF文件夾,然后在創(chuàng)建一個(gè)文件:spring.factories文件加入啟用配置類的路徑,springboot2.7之后有所不同,但是也兼容之前的版本寫法
打包上傳到maven私服(沒搭建的直接install安裝到本地maven倉(cāng)庫(kù)也可)
在別的工程引入,能這樣看到基本就成功了
直接在方法上加@CustomLogAnnotation注解測(cè)試一下
到此為止已經(jīng)成功,想做得好再好點(diǎn)的可以再把日志starter做成動(dòng)態(tài)可插拔
到此這篇關(guān)于如何自定義springboot-starter日志組件供各個(gè)服務(wù)使用(系統(tǒng)日志優(yōu)化)的文章就介紹到這了,更多相關(guān)springboot starter日志組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析Java關(guān)鍵詞synchronized的使用
Synchronized是java虛擬機(jī)為線程安全而引入的。這篇文章主要為大家介紹一下Java關(guān)鍵詞synchronized的使用與原理,需要的可以參考一下2022-12-12SpringBoot項(xiàng)目找不到接口報(bào)404錯(cuò)誤的解決辦法
寫了一個(gè)簡(jiǎn)單的springboot項(xiàng)目,在啟動(dòng)的時(shí)候idea未報(bào)錯(cuò),瀏覽器訪問接口時(shí)報(bào)404的錯(cuò)誤,所以本文給大家介紹了SpringBoot項(xiàng)目找不到接口報(bào)404錯(cuò)誤的解決辦法,文中有相關(guān)的圖文供大家參考,需要的朋友可以參考下2024-12-12java Servlet 實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證碼圖片示例
這篇文章主要介紹了java Servlet 實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證碼圖片示例的資料,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-02-02如何解決IDEA中JSP頁(yè)面部分出現(xiàn)綠色背景色問題
這篇文章主要介紹了如何解決IDEA中JSP頁(yè)面部分出現(xiàn)綠色背景色問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12淺談Ribbon、Feign和OpenFeign的區(qū)別
這篇文章主要介紹了淺談Ribbon、Feign和OpenFeign的區(qū)別。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06spring中FactoryBean中的getObject()方法實(shí)例解析
這篇文章主要介紹了spring中FactoryBean中的getObject()方法實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02SpringBoot集成WebServlet出現(xiàn)自定義servlet請(qǐng)求失敗的問題解決方案
SpringBoot中以Bean方式注冊(cè)Servlet時(shí)遇到的問題,通過(guò)了解DispatcherServlet的原理,發(fā)現(xiàn)默認(rèn)路徑?jīng)_突是主要原因,本文介紹SpringBoot集成WebServlet出現(xiàn)自定義servlet請(qǐng)求失敗的問題解決方案,感興趣的朋友一起看看吧2025-03-03