转载

SpringBoot应用之ELK

SpringBoot应用系列文章

  • SpringBoot应用之配置中心

  • SpringBoot应用之分布式会话

  • SpringBoot应用之分布式索引

  • SpringBoot应用之分布式缓存

  • SpringBoot应用之消息队列

  • SpringBoot应用之ELK

本文主要讲怎么在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

参考

  • spring boot下使用logback或log4j生成符合Logstash标准的JSON格式

  • Manage Spring Boot Logs with Elasticsearch, Logstash and Kibana

  • Log Management for Spring Boot Applications with Logstash, Elasticsearch and Kibana

  • ELK, Docker and Spring Boot

原文  https://segmentfault.com/a/1190000004416276
正文到此结束
Loading...