前言:由於公司業務需求越來越大,和第三方調用的接口也越來越頻繁,有時候需要排查很久之前的日誌。項目初期,排查日誌都是直接去服務器上按時間拷貝下來,然後再搜索對比,過程很是複雜。所以決定架構一個elk日誌檢索系統。該檢索系統應該在項目初期就架構的。主要還是因爲懶,還有就是我是項目中期才進來的。
一、配置log4j2日誌組件
由於原架構使用的是logback作爲日誌組件。當然該日誌組件還是挺好用的,很方便的進行了日誌分割等操作。但是該組件是同步的,所以高訪問的時候可能會影響效率,這裏就換成log4j2支持異步的日誌組件。
1.pom中引入依賴Log4j Starter、disruptor依賴
(1)先exclusions排除掉默認的日誌:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
(2) 引入log4j2相關依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.6</version>
</dependency>
注:disruptor是一個基於無鎖化環形隊列的高性能併發框架,log4j2就是藉助它進行高性能日誌異步輸出的。
2.添加log4j2.xml配置文件
日誌文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug">
<Properties>
<!--自定義一些常量,之後使用${變量名}引用-->
<Property name="logFilePath">logs/activity</Property>
</Properties>
<appenders>
<!--這個輸出控制檯的配置-->
<Console name="STDOUT" >
<!--控制檯只輸出level及以上級別的信息(onMatch),其他的直接拒絕(onMismatch)-->
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" />
<!-- 輸出日誌的格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%logger{50}:%L] [%X{X-B3-TraceId},%X{X-B3-SpanId}] - %msg%n" charset="UTF-8"/>
</Console>
<RollingRandomAccessFile name="FILE-INFO" fileName="${logFilePath}/info/log-info.log"
filePattern="${logFilePath}/info/log-info.%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%logger{50}:%L] [%X{X-B3-TraceId},%X{X-B3-SpanId}] - %msg%n" charset="UTF-8" />
<!-- Policies :日誌滾動策略-->
<Policies>
<!--每1天/小時/分/... 生成一個文件, 時間依據filePattern的配置-->
<TimeBasedTriggeringPolicy interval="1" />
<!-- SizeBasedTriggeringPolicy :文件大小滾動策略-->
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<!-- 默認滾動策略-->
<!-- max參數指定了計數器的最大值。一旦計數器達到了最大值,過舊的文件將被刪除。注意:不要認爲max參數是需要保留的日誌文件的最大數目。
max參數是與filePattern中的計數器%i配合起作用的,其具體作用方式與filePattern的配置密切相關。 -->
<DefaultRolloverStrategy max="3" >
<Delete basePath="logs" maxDepth="2">
<IfFileName glob="*log-info.*.log" />
<!--備註: 這裏的age必須和filePattern協調, 後者是精確到HH, 這裏就要寫成xH, xd就不起作用
另外, 數字最好>2, 否則可能造成刪除的時候, 最近的文件還處於被佔用狀態,導致刪除不成功!-->
<!-- 日誌保留30天的 -->
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="FILE-DEBUG" fileName="${logFilePath}/debug/log-debug.log"
filePattern="${logFilePath}/debug/log-debug.%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" />
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%logger{50}:%L] [%X{X-B3-TraceId},%X{X-B3-SpanId}] - %msg%n" charset="UTF-8" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="3" >
<Delete basePath="logs" maxDepth="2">
<IfFileName glob="*log-debug.*.log" />
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="FILE-WARN" fileName="${logFilePath}/warn/log-warn.log"
filePattern="${logFilePath}/warn/log-warn.%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY" />
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%logger{50}:%L] [%X{X-B3-TraceId},%X{X-B3-SpanId}] - %msg%n" charset="UTF-8" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="3" >
<Delete basePath="logs" maxDepth="2">
<IfFileName glob="*log-warn.*.log" />
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="FILE-ERROR" fileName="${logFilePath}/error/log-error.log"
filePattern="${logFilePath}/error/log-error.%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" />
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%logger{50}:%L] [%X{X-B3-TraceId},%X{X-B3-SpanId}] - %msg%n" charset="UTF-8" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="3" >
<Delete basePath="logs" maxDepth="2">
<IfFileName glob="*log-error.*.log" />
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
</appenders>
<loggers>
<!-- 去掉無關緊要的默認日誌 -->
<AsyncLogger name="springfox" level="OFF"></AsyncLogger>
<!-- includeLocatio輸出行號,有性能開銷 -->
<AsyncRoot level="INFO" includeLocation="true" >
<AppenderRef ref="FILE-INFO" />
<AppenderRef ref="FILE-WARN" />
<AppenderRef ref="FILE-ERROR" />
<AppenderRef ref="STDOUT" />
</AsyncRoot>
</loggers>
</configuration>
3.添加log4j2.component.properties文件
# 環形隊列的大小,默認是26萬
# 全異步模式下
AsyncLogger.RingBufferSize=10000
# 混合模式下
AsyncLoggerConfig.RingBufferSize=10000
# 自動降級--丟棄日誌
log4j2.AsyncQueueFullPolicy=Discard
# 隊列滿後丟棄DEBUG級別的日誌
log4j2.DiscardThreshold=DEBUG
至此log4j2配置完成.
二、Spring Cloud Sleuth接入
1.pom引入相關依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
2.日誌文件配置添加追蹤變量
在log4j2.xml日誌文件中添加: [%X{X-B3-TraceId},%X{X-B3-SpanId}],上述配置文件已添加。
3.在application.properties文件中添加zipkin服務地址
#zipkin服務地址
spring.zipkin.base-url=http://${eureka.instance.hostname}:9411/
#設置抽樣百分比 0.1-1 1爲採集全部信息
spring.sleuth.sampler.probability=1.0
三、Zipkin Server的搭建
1.創建SpringBoot工程
創建zipkin服務,勾選Sleuth和zipkin依賴
2.添加Zipkin Starter、Zipkin Server的相關依賴
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
3.添加application.properties相關依賴
server.port=9411
spring.application.name=zipkin
zipkin.storage.type=mem
4.啓動類添加@EnableZipkinServer註解