SpringBoot使用admin+actuator實現(xiàn)日志可視化的方法
1:創(chuàng)建admin服務(wù)端
pom文件依賴如下
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.1</version>
</dependency>yml配置如下
server: port: 8081
management:
trace:
http:
enabled: true
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always如果需要注冊到nacos或者其他注冊中心,可以按需添加
啟動類需要加上
@EnableAdminServer注解啟動
2:服務(wù)端配置
pom配置如下
dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>yml配置如下
server:
port: 8080 # 微服務(wù)端口
spring:
application:
name: clientServer # 服務(wù)名稱
boot:
admin:
client:
url: http://localhost:8081 # Admin Server地址logging:
config: classpath:logback-spring.xml
file:
name: logs/clientServer/debug.log
pattern:
file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
management:
endpoints:
web:
exposure:
include: "*" # 暴露所有Actuator端點(生產(chǎn)環(huán)境建議按需配置)
endpoint:
health:
show-details: always
logfile:
external-file: ${logging.file.name}
enabled: truelogback-spring中的地址要和yml地址一致,例:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="false">
<springProperty scop="context" name="spring.application.name" source="spring.application.name" defaultValue=""/>
<property name="log.path" value="logs/clientServer" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN"
value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />
<!-- 彩色日志依賴的渲染類 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx"
converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- Console log output -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- Log file debug output -->
<appender name="debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${log.path}/%d{yyyy-MM}/debug.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern>
</encoder>
</appender>
<!-- Log file error output -->
<appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${log.path}/%d{yyyy-MM}/error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
<!-- Level: FATAL 0 ERROR 3 WARN 4 INFO 6 DEBUG 7 -->
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="debug" />
<appender-ref ref="error" />
</root>
</configuration>啟動服務(wù)端,打開http://localhost:8081/
就可以看到注冊進去的服務(wù)了

點擊對應(yīng)的服務(wù)名可以看到服務(wù)對應(yīng)的日志

如果日志頁面報錯404,一定要檢查yml配置的日志路徑和logback-spring.xml中的路徑是否一致
到此這篇關(guān)于SpringBoot使用admin+actuator實現(xiàn)日志可視化的文章就介紹到這了,更多相關(guān)SpringBoot日志可視化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot mybatisplus如何解決分頁組件IPage失效問題
這篇文章主要介紹了Springboot mybatisplus如何解決分頁組件IPage失效問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Java中String.split()的最詳細源碼解讀及注意事項
以前經(jīng)常使用String.split()方法,但是從來沒有注意,下面這篇文章主要給大家介紹了關(guān)于Java中String.split()最詳細源碼解讀及注意事項的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2022-07-07
Java服務(wù)調(diào)用失敗報Service com.oneinfinite.adflow.api.service.T
在Java開發(fā)中,服務(wù)調(diào)用是常見的操作,尤其是在微服務(wù)架構(gòu)中,然而,服務(wù)調(diào)用過程中可能會遇到各種問題,下面我們來看看如何解決Service com.oneinfinite.adflow.api.service.TestService with version 0.0.0 not found的問題吧2025-03-03
SpringBoot與Quartz集成實現(xiàn)分布式定時任務(wù)集群的代碼實例
今天小編就為大家分享一篇關(guān)于SpringBoot與Quartz集成實現(xiàn)分布式定時任務(wù)集群的代碼實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
java用靜態(tài)工廠代替構(gòu)造函數(shù)使用方法和優(yōu)缺點
這篇文章主要介紹了java用靜態(tài)工廠代替構(gòu)造函數(shù)使用方法和優(yōu)缺點,需要的朋友可以參考下2014-02-02
Java使用JNDI連接數(shù)據(jù)庫的實現(xiàn)方法
本文主要介紹了Java使用JNDI連接數(shù)據(jù)庫的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12

