Big Data 5 min read

Step-by-Step Guide to Installing and Configuring the ELK Stack with Spring Boot

This tutorial walks you through setting up ElasticSearch, Kibana, and Logstash, configuring them for a Spring Boot application, and using Kibana to explore indexed logs, providing code snippets, configuration files, and visual instructions for a complete ELK integration.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Step-by-Step Guide to Installing and Configuring the ELK Stack with Spring Boot

ElasticSearch Installation and Configuration

Download the package.

Directory layout explanation.

Start ElasticSearch.

// Run
%elasticsearch_home%\bin\elasticsearch.bat

Check running status.

The default port is 9200; if you changed it for a cluster, edit %elasticsearch_home%\config\elasticsearch.yml to set the new port.

Kibana Installation and Configuration

Download the package.

Configure Kibana.

Edit %kibana_home%\config\kibana.yml with the following settings:

elasticsearch.hosts: ["http://localhost:9201"]
kibana.index: ".kibana"
i18n.locale: "zh-CN"

Start Kibana.

Successful startup.

Logstash Installation and Configuration

Download the package.

Refer to the official documentation for detailed instructions.

Configure Logstash.

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}"
    }
  }
}

This configuration routes logs of type es-demo to ElasticSearch; the index name is created dynamically.

Start Logstash. logstash -f logstash.conf At this point the ELK stack integration is complete.

Spring Boot Project Integration

Dependencies (add to pom.xml).

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>net.logstash.logback</groupId>
  <artifactId>logstash-logback-encoder</artifactId>
  <version>6.6</version>
</dependency>

logback-spring.xml configuration.

Application properties.

logging:
  level:
    com.pack: debug
    org.springframework.data.elasticsearch.core: debug
---
spring:
  profiles:
    active:
    - dev

Test REST 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 value"));
        }
        return "success";
    }
}

After sending a request, view the logs in Kibana.

1. Create an index pattern (images shown below).

You can match the index name exactly or use wildcards, then proceed.

2. Open the Discover tab.

From here you can perform various operations on the logged data.

Setup complete!

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.

loggingSpring 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.