This article describes how the SpringBoot application works with ELK to collect logs.

1. The ELK

1.1 introduction

In the previous article, I wrote an article about ELK log collection scheme. If you are interested, you can go to see it. Click here —–> ELK Log Analysis Scheme.

Here’s a brief description of ELK. ELK has three components that work together to collect logs:

  • ElasticSearch: Stores log information.
  • Logstash: Collects, processes, and forwards log information.
  • Kibana: Provides a searchable Web visual interface.

Now, of course, many of them cooperate with the Beats in use, here don’t do too much description, interested can look at the website, www.elastic.co/cn/products. There are many descriptions of Beats here.

1.2 installation

The author has previously written about ELK installation in a Linux environment, as follows:

  • Install Elasticsearch for Linux
  • Install Logstash for Linux
  • Install Kibana on Linux

In other environments, the installation method is similar. Basically, the installation process is to download the compressed package and decompress it.

2. Output SpringBoot logs to Logstash

Here, logback log is used as an example to create a project and add logstash- Logback – Encoder dependency to the project. The complete POM is shown in the code listing.

<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.1.2. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.dalaoyang</groupId> The < artifactId > springboot_logstash < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < name > springboot_logstash < / name > < description > springboot_logstash < description > < properties > < Java version > 1.8 < / Java version > < / properties > <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.3</version> </dependency> </dependencies> </dependencies> <build> <plugins>  <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

Then create a new logback-spring. XML file and configure the logback log information. Note that the destination attribute configured here is the same as the logstash attribute.

<? xml version="1.0" encoding="UTF-8"? > <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" />

    <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> < destination > 127.0.0.1:4560 < / destination > <! --> <encoder charset="UTF-8"
                class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp>
                    <timeZone>UTC</timeZone>
                </timestamp>
                <pattern>
                    <pattern>
                        {
                        "logLevel": "%level"."serviceName": "${springAppName:-}"."pid": "${PID:-}"."thread": "%thread"."class": "%logger{40}"."rest": "%message"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="LOGSTASH" />
        <appender-ref ref="CONSOLE" />
    </root>

</configuration>

Copy the code

Modify the startup class to include an MVC method for logging, as shown below.

package com.dalaoyang;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringbootLogstashApplication {

    Logger logger = LoggerFactory.getLogger(SpringbootLogstashApplication.class);

    @GetMapping("test")
    public void test(){
        logger.info("Test some initial logs!"); } public static void main(String[] args) { SpringApplication.run(SpringbootLogstashApplication.class, args); }}Copy the code

3. The Logstash configuration

The logstash configuration is as follows. Again, the input must correspond to the one just configured. The output is local ES:

input {
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4560
    codec => json_lines
  }
}
output {
  elasticsearch {
    hosts => "localhost:9200"
    index => "springboot-logstash-%{+YYYY.MM.dd}"}}Copy the code

4. Test

Open the Kibana administration page and add the index you just created, as shown in the figure.

Then go to the discovery page and select the index you just found, as shown below.

Next, in the browser, visit the method that just printed logs in the project several times and query the console, as shown below.

When I go to Kibana, not only the log content, but also the custom attributes are displayed.

5. The source code

Source code address: gitee.com/dalaoyang/s…