0

我已将 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附加程序:

@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 有问题?

4

0 回答 0