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

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

一、代码

1、pom.xml

         <!-- logstash-logback -->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.6</version>
</dependency>

2、application.properties

 #set logstash shipper host
logstash.host=127.0.0.1
#set logstash shipper port
logstash.port=4560
logstash.level=info

3、LogstashProperties.java

 package com.xxx.secondboot.logstash;

 import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import lombok.Getter;
import lombok.Setter; @Component
@ConfigurationProperties(prefix = "logstash")
@Getter
@Setter
public class LogstashProperties {
private String host;
private int port;
private String level;
}

4、LogstashConfig.java

 package com.xxx.secondboot.logstash;

 import java.net.InetSocketAddress;

 import javax.annotation.PostConstruct;

 import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import net.logstash.logback.appender.LogstashTcpSocketAppender;
import net.logstash.logback.encoder.LogstashEncoder; /**
* 该类是可以配置成xml配置文件的,但是那样的话,就不能由客户端指定参数了
*/
@Component
public class LogstashConfig {
@Autowired
private LogstashProperties logstashProperties; @PostConstruct
public void init() {
Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
LogstashTcpSocketAppender appender = new LogstashTcpSocketAppender();
appender.setName("stash");
appender.addDestinations(new InetSocketAddress(logstashProperties.getHost(), logstashProperties.getPort())); LogstashEncoder encoder = new LogstashEncoder();
encoder.setCustomFields("{ \"service\":\"" + "myboot2" + "\"}");//服务名会在日志中显示(可以方便的知道该日志是哪个服务的)
encoder.start(); appender.setEncoder(encoder);
appender.setContext(rootLogger.getLoggerContext());
appender.start();
rootLogger.addAppender(appender);
rootLogger.setLevel(Level.toLevel(logstashProperties.getLevel()));
}
}

注意:

  • 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

 private static final Logger LOGGER = LoggerFactory.getLogger(AdviceController.class);

     @RequestMapping(value = "/testLog", method = RequestMethod.GET)
public String testLog() {
LOGGER.info("test info");
LOGGER.debug("test debug");
LOGGER.error("test error");
LOGGER.warn("test warn");
return "test logstash-logback";
}

二、测试

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

 input {
tcp {
mode => "server"
host => "127.0.0.1"
port => 4560
codec => "json_lines"
}
} filter { } output {
redis{
data_type => "list"
host => ["127.0.0.1:6379"]
key => "microservice:logstash:redis"
}
}

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

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

上一篇:OpenStack 存储服务 Cinder存储节点部署NFS(十七)


下一篇:Spring mvc框架 controller间跳转 ,重定向 ,传参