码迷,mamicode.com
首页 > 其他好文 > 详细

第三十二章 elk(3)- broker架构 + 引入logback

时间:2016-09-21 00:03:53      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:

实际中最好用的日志框架是logback,我们现在会直接使用logback通过tcp协议向logstash-shipper输入日志数据。在上一节的基础上修改!!!

一、代码

1、pom.xml

1         <!-- logstash-logback -->
2         <dependency>
3             <groupId>net.logstash.logback</groupId>
4             <artifactId>logstash-logback-encoder</artifactId>
5             <version>4.6</version>
6         </dependency>

2、application.properties

1 #set logstash shipper host
2 logstash.host=127.0.0.1
3 #set logstash shipper port
4 logstash.port=4560
5 logstash.level=info

3、LogstashProperties.java

 1 package com.xxx.secondboot.logstash;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.stereotype.Component;
 5 
 6 import lombok.Getter;
 7 import lombok.Setter;
 8 
 9 @Component
10 @ConfigurationProperties(prefix = "logstash")
11 @Getter
12 @Setter
13 public class LogstashProperties {
14     private String host;
15     private int    port;
16     private String level;
17 }

4、LogstashConfig.java

 1 package com.xxx.secondboot.logstash;
 2 
 3 import java.net.InetSocketAddress;
 4 
 5 import javax.annotation.PostConstruct;
 6 
 7 import org.slf4j.LoggerFactory;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.stereotype.Component;
10 
11 import ch.qos.logback.classic.Level;
12 import ch.qos.logback.classic.Logger;
13 import net.logstash.logback.appender.LogstashTcpSocketAppender;
14 import net.logstash.logback.encoder.LogstashEncoder;
15 
16 /**
17  * 该类是可以配置成xml配置文件的,但是那样的话,就不能由客户端指定参数了
18  */
19 @Component
20 public class LogstashConfig {
21     @Autowired
22     private LogstashProperties logstashProperties;
23 
24     @PostConstruct
25     public void init() {
26         Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
27         LogstashTcpSocketAppender appender = new LogstashTcpSocketAppender();
28         appender.setName("stash");
29         appender.addDestinations(new InetSocketAddress(logstashProperties.getHost(), logstashProperties.getPort()));
30        
31         LogstashEncoder encoder = new LogstashEncoder();
32         encoder.setCustomFields("{ \"service\":\"" + "myboot2" + "\"}");//服务名会在日志中显示(可以方便的知道该日志是哪个服务的)
33         encoder.start();
34         
35         appender.setEncoder(encoder);
36         appender.setContext(rootLogger.getLoggerContext());
37         appender.start();
38         rootLogger.addAppender(appender);
39         rootLogger.setLevel(Level.toLevel(logstashProperties.getLevel()));
40     }
41 }

注意:

  • init()方法在LogstashConfig bean构建之后执行,之后的日志信息都会写入到LogstashTcpSocketAppender中去
  • 2、3、4其实也可以没有,直接使用xml配置即可(参考:https://github.com/logstash/logstash-logback-encoder/tree/logstash-logback-encoder-4.7),但是这样的话,就无法有我们自己指定logstash参数了,或者需要在xml中指定(这里的配置不在consul中配置),而配在application.properties中(实际上就是配在了consul中)

5、AdviceController.java

 1 private static final Logger LOGGER = LoggerFactory.getLogger(AdviceController.class);
 2 
 3     @RequestMapping(value = "/testLog", method = RequestMethod.GET)
 4     public String testLog() {
 5         LOGGER.info("test info");
 6         LOGGER.debug("test debug");
 7         LOGGER.error("test error");
 8         LOGGER.warn("test warn");
 9         return "test logstash-logback";
10     }

 

二、测试

在上一节的基础上修改logstash-shipper的配置文件:(输入为tcp,配置参考:https://github.com/logstash/logstash-logback-encoder/tree/logstash-logback-encoder-4.7

 1 input {
 2     tcp {
 3         mode => "server"
 4         host => "127.0.0.1"
 5         port => 4560
 6         codec => "json_lines"
 7     }
 8 }
 9 
10 filter {
11     
12 }
13 
14 output {
15     redis{
16         data_type => "list"
17         host => ["127.0.0.1:6379"]
18         key => "microservice:logstash:redis"
19     }
20 }

之后运行启动程序,这时候kibana就会打印出启动日志了,在日志中有servicename的体现,之后访问swagger,我们会发现3条不同级别的日志都进入kibana进行展示了。

技术分享

第三十二章 elk(3)- broker架构 + 引入logback

标签:

原文地址:http://www.cnblogs.com/java-zhao/p/5890986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!