7
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/cat/workspace/elkdemo/log/logstash-2016-02-04.log

kibana查看

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

TODO

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

参考


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...