SpringBoot應用系列文章
本文主要講怎麼在SpringBoot裏頭配置輸出到logstash,使用elk技術棧實時查看日誌。html
詳見docker環境搭建ELKjava
<dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>4.4</version> </dependency>
<?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>
@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()); } } }
nc 192.168.99.100 5000 < /Users/cat/workspace/elkdemo/log/logstash-2016-02-04.log
http://192.168.99.100:5601/
git
剛纔那個nc到logstash的是靜態導入的,須要配置動態導入log,以便實時查看。github