How to Set Up the ELK Stack with Spring Boot 2.4.12

Learn step‑by‑step how to install and configure Elasticsearch, Kibana, and Logstash, integrate them with a Spring Boot 2.4.12 application, and use Kibana to view logs, covering environment setup, service startup, configuration files, and sample code.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Set Up the ELK Stack with Spring Boot 2.4.12

Environment: Spring Boot 2.4.12, Elasticsearch 7.8.0, Logstash 7.8.0, Kibana 7.8.0.

Elasticsearch Installation and Configuration

Download the package.

Start Elasticsearch with %elasticsearch_home%\bin\elasticsearch.bat.

Check the running status (default port 9200, adjust in %elasticsearch_home%\config\elasticsearch.yml if needed).

Kibana Installation and Configuration

Download the package.

Edit %kibana_home%\config\kibana.yml (e.g., set elasticsearch.hosts: ["http://localhost:9201"], kibana.index: ".kibana", i18n.locale: "zh-CN").

Start Kibana and verify the success message.

Logstash Installation and Configuration

Download the package.

Create %logstash_home%\bin\logstash.conf with the following content:

input {
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4560
    codec => json_lines
    type => "es-demo"
  }
}
output {
  if [type] == "es-demo" {
    elasticsearch {
      hosts => "localhost:9201"
      index => "elk-demo-%{+YYYY.MM.dd}"
    }
  }
}

Start Logstash with logstash -f logstash.conf.

At this point the ELK integration is complete.

Spring Boot Project

Add Maven dependencies for spring-boot-starter-web and logstash-logback-encoder (version 6.6).

Configure logback-spring.xml as needed.

Set application properties (logging levels, active profile).

Create a test controller:

@RestController
@RequestMapping("/elk")
public class ElkController {
    private static Logger logger = LoggerFactory.getLogger(ElkController.class);
    @GetMapping("/index")
    public Object index(String info, Integer a) {
        logger.info("You entered: {}", info);
        if (a == 0) {
            logger.error("Error occurred: {}", new RuntimeException("Invalid number"));
        }
        return "success";
    }
}

After running the application, use Kibana to create an index pattern, explore the logs in the Discover view, and perform various operations.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ElasticsearchloggingSpring BootELKLogstashKibana
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.