如何将Spring Boot日志直接发送到Elasticsearch服务器?

H A*_*ala 7 java logging elasticsearch spring-boot

我\xe2\x80\x99m 正在寻找一种最佳/最新的方法来将 Spring Boot 应用程序日志直接发送到 Elasticsearch 服务器,而不使用 Filebeats 或 Logstash。我怎样才能做到这一点?Spring Boot 中是否有一种简单/现代的方法或使用任何好的/知名的库来实现这一目标?

\n

我需要的是直接将日志从 Spring Boot 发送到 Elasticsearch,而不需要任何像 Logstash 这样的中间服务。如果第三方库可以添加pom.xml并且它完全做到了这一点那就没问题了。我需要 Spring 应用程序本身来处理这个问题。我在Stack Overflow上查过一些类似的问题。

\n

但有些库现在已被弃用,有些库已经很长时间没有更新了。我想知道一个新的图书馆或者现在有什么方法可以做到这一点?基本上,写入控制台的内容应该发送到 Elasticsearch。

\n

dev*_*ock 11

您可以在 pom.xml 中包含logback-elastic-appender库,并使用包含的类作为文件com.internetitem.logback.elasticsearch.ElasticsearchAppender中日志记录配置中的附加程序logback.xml

<dependency>
    <groupId>com.internetitem</groupId>
    <artifactId>logback-elasticsearch-appender</artifactId>
    <version>1.6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这是您不想使用的库之一,因为它最近没有更新吗?如果是这样,您可以编写一个自定义附加程序并在文件中指向它logback.xml。附加器的简单实现如下所示:

package com.example.demo;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.web.reactive.function.client.WebClient;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import reactor.core.publisher.Mono;

public class ElasticSearchAppender extends AppenderBase<ILoggingEvent> {
    private static final String ELASTIC_SEARCH_API_HOST = "http://localhost:9200";
    private static final String ELASTIC_SEARCH_INDEX_NAME = "dummy-index";
    private static final WebClient webClient = WebClient.create(ELASTIC_SEARCH_API_HOST);
    private static final Logger LOGGER = Logger.getLogger(ElasticSearchAppender.class.getName());
    public static final DateTimeFormatter ISO_8601_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
            .withZone(ZoneId.systemDefault());

    @Override
    protected void append(ILoggingEvent eventObject) {
        Map<String, Object> loggingEvent = new LinkedHashMap<>();
        loggingEvent.put("@timestamp", 
                ISO_8601_FORMAT.format(Instant.ofEpochMilli(eventObject.getTimeStamp())));
        loggingEvent.put("message", eventObject.getMessage());
        
        // Add additional fields like MDC
        
        webClient.post()
            .uri("/{logIndex}/_doc", ELASTIC_SEARCH_INDEX_NAME)
            .bodyValue(loggingEvent)
            .retrieve()
            .bodyToMono(Void.class)
            .onErrorResume(exception -> {
                LOGGER.log(Level.SEVERE, "Unable to send log to elastic", exception);
                return Mono.empty();
            })
            .subscribe();
    }

}
Run Code Online (Sandbox Code Playgroud)

日志回.xml:

<configuration>
    <appender name="ELASTIC" class="com.example.demo.ElasticSearchAppender" />
    <root level="INFO">
        <appender-ref ref="ELASTIC"/>
    </root>
</configuration>
Run Code Online (Sandbox Code Playgroud)


sem*_*lon -6

您可以使用Spring boot提供的LOGGER:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
    
private static final Logger LOGGER = LogManager.getLogger(yourClass.class);

LOGGER.info("PRINT THIS OUT");
Run Code Online (Sandbox Code Playgroud)

这应该显示在 elastic beanstalk 中名为“Messages”的文件中。

这来自 spring-boot-starter-web 依赖项。

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)