ELK for SpringBoot application

  elasticsearch, elk, kibana, logstash, springboot

SpringBoot application series articles

Order

This article mainly talks about how to configure output to logstash in SpringBoot and use elk technology stack to view logs in real time.

Prepare elk

For details, seeDocker Environment Builds ELK

New project

图片描述

Add dependency

        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>4.4</version>
        </dependency>

Configure logback

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>
<configuration>
    <appender name="LOGSTASH" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>log/logstash-%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
    </appender>

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

Log instance

@SpringBootApplication
public class ElkdemoApplication implements CommandLineRunner{

    private static final Logger logger = LoggerFactory.getLogger(ElkdemoApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ElkdemoApplication.class, args);
    }

    @Autowired
    LogDemoService logDemoService;

    @Override
    public void run(String... strings) throws Exception {
        while(true){
            logDemoService.generateLog();
            Thread.sleep(1000);
            logger.info("current thread:{},content:{}",Thread.currentThread().getName(), UUID.randomUUID().toString());
        }
    }
}

Import logstash

nc 192.168.99.100 5000 < /Users/cat/workspace/elkdemo/log/logstash-2016-02-04.log

Kibana view

http://192.168.99.100:5601/
图片描述

TODO

The nc to logstash just now was imported statically, and the dynamic import log needs to be configured for real-time viewing.

References