赞
踩
目录
关于日志监控、日志管理,对于任何一个成型的系统来说都是必不可少的,之前使用Commons-IO实现过日志监控功能,最终是数据存在MongoDB。实践告诉我们这个过程很繁琐,当然,这里并不是难实现的意思。 可是实际应用中一般没人选择这种方式。 听说使用ElasticStack 可以很好的达到实现我们的目的,于是决定尝试了一下,这里对做一个简单的记录,希望对你有一定的帮助。
Elastic Stack 介绍_To Do.的博客-CSDN博客
开始操作之前,请确保软件安装启动正常,以此结合如下配置和流程实现最终的效果。
分别启动fileBeat、logstash、es(单节点)、kibana,确保容器都启动成功,效果如下
使用Docker方式搭建ElasticStack:【图文详解】Docker搭建 ELK Stack (elk) [使用es-logstash-filebeat-kibana]_hah杨大仙的博客-CSDN博客
必要的依赖
- <dependency>
- <groupId>joda-time</groupId>
- <artifactId>joda-time</artifactId>
- <version>2.9.9</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.12.0</version>
- </dependency>
启动类
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.RandomUtils;
- import org.joda.time.DateTime;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @Slf4j
- @SpringBootApplication
- public class ElkBwGenerateApplication {
-
- public static final String[] VISIT = new String[]{
- "浏览页面","评论商品","加入收藏","加入购物车","提交订单","使用优惠券","领取优惠券","搜索","查看订单"
- };
-
- public static void main(String[] args) throws Exception {
- SpringApplication.run(ElkBwGenerateApplication.class, args);
- while (true) {
- long sleep = RandomUtils.nextLong(200, 1000 * 5);
- Thread.sleep(sleep);
-
- Long maxUserId = 9999L;
-
- Long userId = RandomUtils.nextLong(1,maxUserId);
- String visit = VISIT[RandomUtils.nextInt(0,VISIT.length)];
-
- DateTime now = new DateTime();
-
- int maxHour = now.getHourOfDay();
- int maxMinutes = now.getMinuteOfHour();
- int maxSeconds = now.getSecondOfMinute();
-
- String date = now.plusHours(-(RandomUtils.nextInt(0, maxHour)))
- .plusMinutes(-(RandomUtils.nextInt(0, maxMinutes)))
- .plusSeconds(-(RandomUtils.nextInt(0, maxSeconds)))
- .toString("yyyy-MM-dd HH:mm:ss");
-
- String result = "DAU|" + userId + "|" +visit +"|"+date;
-
- log.info(result);
- }
- }
-
- }

日志框架使用LogBack,注意记录logBack的日志文件产出路径,后续需要用到。
========================================================================
查看日志文件内容
========================================================================
logstash.conf
- input {
- beats {
- port => "5678"
- }
- }
-
- # 过滤器
- filter {
- # 对内容进行切割
- mutate {
- split => {"message"=>"|"}
- }
- # 将切割后的部分,分别添加成为字段
- mutate {
- add_field =>{
- "userId" => "%{[message][1]}"
- "visit" => "%{[message][2]}"
- "date" => "%{[message][3]}"
- }
- }
- # 字段添加类型
- mutate {
- convert => {
- "userId" => "integer"
- "visit" => "string"
- "date" => "string"
- }
- }
- }
-
- # 输出到控制台
- #output {
- # stdout { codec => rubydebug }
- #}
-
- # 输出到es
- output {
- elasticsearch {
- hosts => ["http://192.168.241.128:9200"]
- }
- }

logstash.yml
========================================================================
正常启动,并查看数据
========================================================================
正常启动,访问 ip:5601
分别添加柱形图、饼图,并创建仪表盘
========================================================================
========================================================================
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。