【详解】 ELK (ElasticStack) 实现日志监控

目录

ElasticStack 介绍:

Demo 实现

说在前面

案例实现流程图

创建Spring Boot 项目

项目部署、运行

Logstash配置

FileBeat配置

ES配置

Kibana配置

最终效果


前言

关于日志监控、日志管理,对于任何一个成型的系统来说都是必不可少的之前使用Commons-IO实现过日志监控功能,最终是数据存在MongoDB。实践告诉我们这个过程很繁琐,当然,这里并不是难实现的意思。 可是实际应用中一般没人选择这种方式。  听说使用ElasticStack 可以很好的达到实现我们的目的,于是决定尝试了一下,这里对做一个简单的记录,希望对你有一定的帮助。 

Commons-IO实现日志监控:【详解】日志监控_To Do.的博客-CSDN博客_监控日志简介:日志监控提供了针对日志内容的实时监控能力。通过云监控服务和云日志服务的结合,用户可以针对日志内容进行监控统计、设置告警规则等,降低用户监控日志的运维成本,简化用户使用监控日志的流程。前情提要:本文主要针对日志文件的监控、解析、入库,至于后续统计、警告等等操作,并没有明确说明,如果要完成整套的日志框架,建议使用ELK框架,而不是手动去写一个日志系统。ELK简介参见:【ELK学习笔记】ELK的简介_大龄码农生活的博客-CSDN博客_elk学习核心思路:利用Commons...https://blog.csdn.net/weixin_47255175/article/details/124055243

ElasticStack 介绍:

Elastic Stack 介绍_To Do.的博客-CSDN博客

Demo 实现

说在前面

 开始操作之前,请确保软件安装启动正常,以此结合如下配置和流程实现最终的效果。

分别启动fileBeat、logstash、es(单节点)、kibana,确保容器都启动成功,效果如下

使用Docker方式搭建ElasticStack:【图文详解】Docker搭建 ELK Stack (elk) [使用es-logstash-filebeat-kibana]_hah杨大仙的博客-CSDN博客

案例实现流程图

创建Spring Boot 项目

必要的依赖

<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配置

========================================================================

 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

FileBeat配置

========================================================================

 

ES配置

正常启动,并查看数据

 ========================================================================

Kibana配置

正常启动,访问 ip:5601

分别添加柱形图、饼图,并创建仪表盘

========================================================================

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

最终效果

 ========================================================================

  • 4
    点赞
  • 88
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

To Do.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值