我已将sleuth / zipkin添加到我的项目中。我使用的是logback,默认情况下,我在控制台和文件中也可以获得格式良好的日志。我也使用了logstash appender,当我看到kibana如何呈现这些日志时 - 我根本不满意。以下是详细信息:
的pom.xml:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
...
</dependencies>
我的logstash appender:
@Component
@ConfigurationProperties(prefix = "logstash")
public class LogstashConfig {
private String uri;
@Value("${spring.application.name}")
private String applicationName;
public void setUri(String uri) {
this.uri = uri;
}
@PostConstruct
public void init() {
final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
final LoggerContext loggerContext = rootLogger.getLoggerContext();
final LogstashTcpSocketAppender logstashTcpSocketAppender = new LogstashTcpSocketAppender();
logstashTcpSocketAppender.setName(applicationName);
logstashTcpSocketAppender.setContext(loggerContext);
logstashTcpSocketAppender.addDestination(uri);
final LogstashEncoder encoder = new LogstashEncoder();
encoder.setContext(loggerContext);
encoder.setCustomFields("{\"application_name\":\"" + applicationName + "\"}");
encoder.start();
logstashTcpSocketAppender.setEncoder(encoder);
logstashTcpSocketAppender.start();
rootLogger.addAppender(logstashTcpSocketAppender);
rootLogger.setLevel(Level.INFO);
rootLogger.info("Logstash succesfully configured: application connected to logstatsh server {}", uri);
}
}
这就是我在kibana看到的:
LOG_LEVEL_PATTERN:%clr(%5p) %clr([my-app-name,%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]){yellow} application_name:my-app-name
日志模式解决的唯一问题是应用程序名称。我错过了一些配置吗?或者我的logstash appender中可能出现了问题?