作者:DEV
出自:解道JDON
原文:jdon.com/55006
今天,我们有了一类称为日志聚合系统的应用程序。当我们拥有大量微服务并且我们希望跨微服务跟踪日志时,日志聚合很有用。
传统的Java应用程序日志如下所示:
2020-09-17 21:56:10.740 INFO [Orders:restartedMain::] o.s.b.w.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
使用下面方法对日志进行搜索:
cat application.log | grep <your-search>
有人想出了这种不同的方法,为什么在主要用于搜索时为什么不以更可搜索的格式存储日志。JSON碰巧很容易被搜索到,从而进行JSON日志记录。另一种引用方式是结构化日志,因为日志具有定义明确的结构,以后可以用来搜索。
以JSON格式显示的相同应用程序日志如下所示:
{"@timestamp": "2020-06-17T14:41:11.174-04:00","@version": "1","message": "Tomcat initialized with port(s): 8080 (http)","logger_name": "org.springframework.boot.web.embedded.tomcat.TomcatWebServer","thread_name": "restartedMain","level": "INFO","level_value": 20000
}
要以JSON格式记录日志,需要包含2个依赖项。如果您使用maven进行依赖性管理,则将包括以下依赖性
<dependency><groupId>net.logstash.logback</groupId><artifactId>logstash-logback-encoder</artifactId><version>6.4</version>
</dependency>
<dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version>
</dependency>
在这里,我们使用的是logstash-logback-encoder库
然后,我们可以使用以下代码段将json编码器配置为logback
<encoder class="net.logstash.logback.encoder.LogstashEncoder"><providers><timestamp><timeZone>EST</timeZone></timestamp><pattern><pattern>{"level": "%level","service": "orders","traceId": "%X{X-B3-TraceId:-}","spanId": "%X{X-B3-SpanId:-}","thread": "%thread","class": "%logger{40}","message": "%message"}</pattern></pattern><stackTrace><throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter"><maxDepthPerThrowable>30</maxDepthPerThrowable><maxLength>2048</maxLength><shortenedClassNameLength>20</shortenedClassNameLength><rootCauseFirst>true</rootCauseFirst></throwableConverter></stackTrace></providers>
</encoder>
因此,让我们假设您想将数据作为传统日志记录到控制台,并将它们作为json记录到文件,我们可以如下配置logback xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration><include resource="org/springframework/boot/logging/logback/console.xml"/><appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [Orders:%thread:%X{X-B3-TraceId}:%X{X-B3-SpanId}] %logger{40} - %msg%n</pattern></encoder></appender><appender name="fileout"class="ch.qos.logback.core.rolling.RollingFileAppender"><File>./logs/orders.log</File><rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"><maxIndex>8</maxIndex><FileNamePattern>./logs/orders.log.%i</FileNamePattern></rollingPolicy><triggeringPolicyclass="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"><MaxFileSize>128MB</MaxFileSize></triggeringPolicy><encoder class="net.logstash.logback.encoder.LogstashEncoder"><providers><timestamp><timeZone>EST</timeZone></timestamp><pattern><pattern>{"level": "%level","service": "orders","traceId": "%X{X-B3-TraceId:-}","spanId": "%X{X-B3-SpanId:-}","thread": "%thread","class": "%logger{40}","message": "%message"}</pattern></pattern><stackTrace><throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter"><maxDepthPerThrowable>30</maxDepthPerThrowable><maxLength>2048</maxLength><shortenedClassNameLength>20</shortenedClassNameLength><rootCauseFirst>true</rootCauseFirst></throwableConverter></stackTrace></providers></encoder></appender><root level="info"><appender-ref ref="fileout" /><appender-ref ref="stdout" /></root>
</configuration>