First, environmental preparation

Install Elasticsearch, Kibana, LogStash

Configure the SpringBoot dependency

Add logStash dependencies to POM files under the springBoot project

<! --logStash--> <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> The < version > 5.3 < / version > < / dependency >Copy the code

Add the log data configuration file logback-spring. XML to the Springboot resource folder

<? 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"> <! - the configurationlogStash service address --> <destination>192.168.92.50: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

Configure the Logstash log collection configuration

Add log collection configuration information to the logstash configuration file logstash. Conf

# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.

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

Note the host address and ES address of the above configuration and use this configuration file to start the Logstash service.

Kibana log filtering configuration and display

Start the SpringBoot service and configure the log interceptor in Kibana