1.下载Logstash 7.3.1,解压,在config目录创建springboot-log.conf,
input{
tcp {
mode => "server"
host => "0.0.0.0"
port => 8888
codec => json_lines
}
}
output{
#elasticsearch{
# hosts=>["127.0.0.1:9200"]
# index => "springboot"
#}
stdout{
codec => rubydebug
}
}
2.启动Logstash,
binlogstash.bat -f config/springboot-log.conf
Linux环境:
bin/logstash -f config/springboot-log.conf
3.创建spring boot应用,并在resources中创建logback-spring.xml
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n localhost:8888
4.在pom.xml中增加依赖,
net.logstash.logback logstash-logback-encoder 7.0
5.添加controller,并在其中增加接口,
package cn.edu.tju.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class TestController {
protected final static Logger log = LoggerFactory.getLogger(TestController.class);
@RequestMapping("/test")
public String test() {
log.info("this is a log from spring boot!");
return new Date().toLocaleString();
}
}
6.启动应用,并访问http://localhost:8085/test,
在Logstash的输出可以看到日志输出:



