SpringBoot应用之ELK

459 查看

SpringBoot应用系列文章

本文主要讲怎么在SpringBoot里头配置输出到logstash,使用elk技术栈实时查看日志。

准备elk

详见docker环境搭建ELK

新建项目

新增依赖

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

配置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实例

@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());
        }
    }
}

导入logstash

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

kibana查看

http://192.168.99.100:5601/

TODO

刚才那个nc到logstash的是静态导入的,需要配置动态导入log,以便实时查看。

本工程github

参考


2018 - 知识虫 - 我的知识库 渝ICP备16002641号-2

渝公网安备 50010702501581号