logstash收集springboot日志
logstash收集springboot日志
maven依赖
net.logstash.logback
logstash-logback-encoder
5.1
springboot 配置文件
logging:
config: classpath:logback.xml
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
${LOG_APP_HOME}/base.%d{yyyy-MM-dd}.log
${LOG_MAX_HISTORY}
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{500} - %msg%n
localhost:4567
logstash
springboot-log.conf
# 文档
# https://www.elastic.co/guide/en/logstash/5.6/input-plugins.html
# https://www.elastic.co/guide/en/logstash/6.1/input-plugins.html
input{
tcp {
mode => "server"
host => "0.0.0.0"
port => 4567
codec => json_lines
}
}
output{
# 为了模拟测试就先不放es了,在控制台输出测试看看
#elasticsearch{
# hosts=>["127.0.0.1:9200"]
# index => "springboot-elk-%{+YYYY.MM.dd}"
#}
stdout{
codec => rubydebug
}
}
测试接口
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@RestController
public class ApiTests {
protected final static Logger log = LoggerFactory.getLogger(ApiTests.class);
@GetMapping("/api/log")
public Object log() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
String ip = request.getRemoteAddr();
String uri = request.getRequestURI();
String logStr = String.format("IP=[%s] send request URI = [%s]", ip, uri);
log.debug("[debug] "+logStr);
log.info("[info] "+logStr);
log.warn("[warn] "+logStr);
log.error("[error] "+logStr);
return "ok : "+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
}
测试效果
先运行logstash
# linux:
bin/logstash -f config/springboot-log.conf
# windows:
bin\logstash.bat -f config/springboot-log.conf
在运行springboot项目,然后调用测试接口:http://localhost:8080/api/log
观察logstash
控制台输出:
{
"@timestamp" => 2021-04-28T15:40:43.182Z,
"level" => "DEBUG",
"port" => 57778,
"thread_name" => "http-nio-8080-exec-1",
"level_value" => 10000,
"@version" => "1",
"host" => "127.0.0.1",
"logger_name" => "com.bart.elk.ApiTests",
"message" => "[debug] IP=[127.0.0.1] send request URI = [/api/log]"
}
{
"@timestamp" => 2021-04-28T15:40:43.182Z,
"level" => "INFO",
"port" => 57778,
"thread_name" => "http-nio-8080-exec-1",
"level_value" => 20000,
"@version" => "1",
"host" => "127.0.0.1",
"logger_name" => "com.bart.elk.ApiTests",
"message" => "[info] IP=[127.0.0.1] send request URI = [/api/log]"
}
{
"@timestamp" => 2021-04-28T15:40:43.183Z,
"level" => "WARN",
"port" => 57778,
"thread_name" => "http-nio-8080-exec-1",
"level_value" => 30000,
"@version" => "1",
"host" => "127.0.0.1",
"logger_name" => "com.bart.elk.ApiTests",
"message" => "[warn] IP=[127.0.0.1] send request URI = [/api/log]"
}
{
"@timestamp" => 2021-04-28T15:40:43.183Z,
"level" => "ERROR",
"port" => 57778,
"thread_name" => "http-nio-8080-exec-1",
"level_value" => 40000,
"@version" => "1",
"host" => "127.0.0.1",
"logger_name" => "com.bart.elk.ApiTests",
"message" => "[error] IP=[127.0.0.1] send request URI = [/api/log]"
}
说明运行成功!
参考说明:
logstash-logback-encoder
logstash 文档