大数跨境
0
0

使用Filebeat收集springboot项目日志到elasticsearch(EFK)

使用Filebeat收集springboot项目日志到elasticsearch(EFK) Spring全家桶实战案例
2021-05-24
0
导读:使用Filebeat收集springboot项目日志到elasticsearch(EFK)

环境:springboot2.3.10 + elasticsearch7.8.0 + kibana7.8.0 + filebeat7.8.0


安装Elasticsearch

参考《Springboot整合ELK日志收集详解步骤

安装Kibana

参考《Springboot整合ELK日志收集详解步骤 》

安装filebeat

步骤如下:


  • 下载


在上面地址下载自己需要的版本,我这里都是用的7.8.0整套。

  • 解压filebeat


我们需要通过filebeat将日志信息收集放入es中,这里修改filebeat.yml配置文件

filebeat.inputs:
- type: log
enabled: true
paths:
- E:\sts6_projects\logs\es-elk\*.log #这里配置为我们项目生成的日志目录即可
tags: ["file"]
multiline:
pattern: ^[0-9]{4} #这里的意义是,不是以日期开头的日志信息会合并到上一行
negate: true
match: after
timeout: 3s
#下面这些配置是修改索引的前缀
setup.ilm.enabled: false
setup.template.name: "sb"
setup.template.pattern: "sb-log-*"
setup.template.settings:
index.number_of_shards: 1
index.codec: best_compression
#配置输出到es中
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["localhost:9200"]
index: "efb-%{+yyyy.MM.dd}" #索引会每天生成一个
processors:
- script:
lang: javascript
id: my_filter
tag: enable
source: >
function process(event) {
var str= event.Get("message");
var time =str.split(" ").slice(0,2).join(" ");
event.Put("start_time",time);
}
# 将自定义的字段替换到系统默认时间戳,解决顺序错乱问题(写入时间与抓取时间不一致导致)
- timestamp:
field: start_time
timezone: Asia/Shanghai
layouts:
- '2021-01-01 12:12:05'
- '2021-01-02 12:12:05.999'
test:
- '2021-01-02 23:59:59'
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~

更多的配置信息请阅读下面链接:


根据上面的配置修改完毕后启动filebeat,通过命令行

filebeat.exe -e -c filebeat.yml


启动成功!!!

通过Kibana查看索引


创建了efb-*索引。

Springboot项目接口

@RestController
@RequestMapping("/elk")
public class ElkController {

private static Logger logger = LoggerFactory.getLogger(ElkController.class) ;

@GetMapping("/index")
public Object index(String info, Integer a) {
logger.info("你输入的是:{}", info) ;
if (a == 0) {
logger.error("发生错误了: {}", new RuntimeException("数值不正确")) ;
}
return "success" ;
}

}

启动服务后,通过Kibana查看日志信息


已经可以看到项目的启动日志信息了。访问接口


正常信息,再来个异常信息


异常信息也完整地展现出来了。

完毕!!!


【声明】内容源于网络
0
0
Spring全家桶实战案例
Java全栈开发,前端Vue2/3全家桶;Spring, SpringBoot 2/3, Spring Cloud各种实战案例及源码解读
内容 832
粉丝 0
Spring全家桶实战案例 Java全栈开发,前端Vue2/3全家桶;Spring, SpringBoot 2/3, Spring Cloud各种实战案例及源码解读
总阅读38
粉丝0
内容832