SpringBoot使用admin+actuator實(shí)現(xiàn)日志可視化的方法
1:創(chuàng)建admin服務(wù)端
pom文件依賴(lài)如下
<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如果需要注冊(cè)到nacos或者其他注冊(cè)中心,可以按需添加
啟動(dòng)類(lèi)需要加上
@EnableAdminServer注解啟動(dòng)
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ù)名稱(chēng)
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端點(diǎn)(生產(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}}" />
<!-- 彩色日志依賴(lài)的渲染類(lèi) -->
<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>啟動(dòng)服務(wù)端,打開(kāi)http://localhost:8081/
就可以看到注冊(cè)進(jìn)去的服務(wù)了

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

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

