ELK是Elastic公司的三个组件,他们三个配合实现日志收集.
有关ELK安装请参考我之前的教程.
Windows下Logstash安装:
https://blog.csdn.net/syc000666/article/details/98527232
Windows下ElasticSearch安装:
https://blog.csdn.net/syc000666/article/details/94664999
Windows下Kibana安装:
https://blog.csdn.net/syc000666/article/details/94665625
进入到logstash安装目录下,我的目录为E:\ELK\logstash-5.6.8\bin目录.

在该目录下创建一个Logstash.conf配置文件,文件内容如下:
input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
}
}
output {
elasticsearch {
hosts => "localhost:9200"
index => "springboot-logstash-%{+YYYY.MM.dd}"
}
}
logstash -f logstash.conf
net.logstash.logback
logstash-logback-encoder
5.3
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
1.16.22
新建一个logback-spring.xml文件,配置logback日志信息,注意这里配置的destination属性,输出的要和logstash配置的对应上,不然收集不上,内容如下:
INFO
${CONSOLE_LOG_PATTERN}
utf8
127.0.0.1:4560
UTC
{
"logLevel": "%level",
"serviceName": "${springAppName:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger{40}",
"rest": "%message"
}
package com.syc.boot;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class LogController {
@GetMapping("/test")
public String showLog(){
log.warn("日志测试,啦啦啦啦,lalala");
return "ok";
}
}
package com.syc.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LogApplication {
public static void main(String[] args){
SpringApplication.run(LogApplication.class,args);
}
}
打开kibana管理页面,添加刚刚创建的索引,如图所示:

接下来在浏览器多次访问刚刚在项目中输出日志的方法,查询控制台,如下所示.

然后在进入kibana查看,不光是日志内容,还有自定义的属性也显示出来了.

至此,我们在SpringBoot中整合ELK实现了日志收集.