react 前端框架如何驱动企业数字化转型与创新发展
1088
2023-01-24
一文秒懂logstash收集springboot日志的方法
maven依赖
springboot 配置文件
logging:
config: classpath:logback.xml
logback.xml
class="ch.qos.logback.core.rolling.RollingFileAPPender">
class="ch.qos.logback.core.rolling.RollingFileAppender">
logstash
springboot-log.conf
# 文档
# https://elastic.co/guide/en/logstash/5.6/input-plugins.html
# https://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
参考博客-1
参考博客-2
logstash 文档
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~