SpringBoot應用之ELK

SpringBoot應用系列文章

本文主要講怎麼在SpringBoot裏頭配置輸出到logstash,使用elk技術棧實時查看日誌。html

準備elk

詳見docker環境搭建ELKjava

新建項目

圖片描述

新增依賴

<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/
圖片描述git

TODO

剛纔那個nc到logstash的是靜態導入的,須要配置動態導入log,以便實時查看。github

參考

相關文章
相關標籤/搜索