轻量级日志管理系统SpringBoot3+Loki+grafana的使用实例

server/2024/9/30 0:10:40/

目录

文章目录

  • 目录
  • 1、简介
  • 2、SpringBoot3应用发送日志到Loki
    • 2.1、基本介绍
    • 2.2、添加依赖
    • 2.3、配置文件application.yml
    • 2.4、创建logback配置
    • 2.5、添加日志示例
    • 2.6、运行SpringBoot3
  • 3、在grafana中查看日志
    • 3.1、登录grafana
    • 3.2、查询日志
    • 3.3、查询我们的SpringBoot发送过来的日志
    • 3.4、按日志级别查询
    • 3.5、按主机查询

1、简介

通过集成日志工具Loki+Promtail使得能够自动化采集日志。

Grafana作为可视化终端,通过链接Loki数据源,能够对采集的日志进行搜索和分析。

其中:

  • Loki: 日志聚合工具,类似ELK中Elasticsearch

  • Promtail: 日志收集工具,类比ELK中的Logstash

  • Grafana:可视化工具,类比ELK中Kibana

效果图
在这里插入图片描述

2、SpringBoot3应用发送日志到Loki

2.1、基本介绍

通过在SpringBoot3中配置logback,主要的配置是使用logback-spring.xml文件配置日志发送到loki

2.2、添加依赖

       <!-- logstash-logback-encoder --><dependency><groupId>net.logstash.logback</groupId><artifactId>logstash-logback-encoder</artifactId><version>7.4</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId></dependency><!-- loki-logback-appender --><dependency><groupId>com.github.loki4j</groupId><artifactId>loki-logback-appender</artifactId><version>1.5.0</version></dependency>

2.3、配置文件application.yml

这里注意是配置一个 spring.application.name 在logback中使用,发送到Loki中以后方便标识

spring:application:name: Taxsoft_SpringBoot3_Loki

2.4、创建logback配置

在项目的资料目录创建: src\main\resources\logback-spring.xml

(1)指定loki服务器URL地址http://localhost:3100

(2)Loki不索引日志的内容,而只索引元数据标签。有一些静态标签,如应用程序名称、日志级别或主机名。我们可以在format.label字段中设置它们。

(3)设置一些动态标签,因为我们启用了Logback标签功能。

(4)设置日志格式模式。

为了简化LogQL(Loki查询语言)的潜在转换,我们将使用JSON表示法。

内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration><!--  配置App日志的名称  --><springProperty name="name"  source="spring.application.name"/><!--  配置控制台输出  --><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36} %X{X-Request-ID} - %msg%n</pattern></encoder></appender><!-- 配置loki4j   --><appender name="LOKI" class="com.github.loki4j.logback.Loki4jAppender"><!-- 1、 指定loki服务器URL--><http><url>http://172.23.1.235:3100/loki/api/v1/push</url></http><format><!-- 2、 配置标签 --><label><pattern>app=${name},host=${HOSTNAME},level=%level</pattern><!-- 3、读取标记--><readMarkers>true</readMarkers></label><message><!-- 4、格式--><pattern>{"level":"%level","class":"%logger{36}","thread":"%thread","message": "%message","requestId": "%X{X-Request-ID}","datetime": "%d{yyyy-MM-dd HH:mm:ss.SSS}"}</pattern></message></format></appender><root level="INFO"><appender-ref ref="CONSOLE"/><appender-ref ref="LOKI"/></root></configuration>

2.5、添加日志示例

除了静态标签,我们还可以发送动态数据,例如仅针对当前请求的特定数据。假设我们有一个管理人员的服务,我们希望记录请求中目标人员的id。正如我之前提到的,使用Loki4j,我们可以使用Logback标记。在经典的Logback中,标记主要用于过滤日志记录。使用Loki,我们只需要定义LabelMarker对象:

(1)该对象包含动态字段的键/值映射。

(2)然后我们将对象传递到当前日志行。

和以前的使用方式一样

我这里写了一个控制器

package com.ts.controller;import com.github.loki4j.slf4j.marker.LabelMarker;
import lombok.extern.java.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;//@Log
@RestController
public class MainController {private static final Logger log = LoggerFactory.getLogger(MainController.class);@RequestMapping("/")public String index() {log.info("这是一条info信息消息");log.error("This is an error message");return "Hello";}@RequestMapping("/test")public String test() {log.warn("这是一条警告信息。。。。");//动态标签//动态字段的键/值映射LabelMarker marker = LabelMarker.of("personId", () ->String.valueOf(1001));//将对象传递到当前日志行log.info(marker,"用户已成功更新");return "Test LabelMarker";}
}

2.6、运行SpringBoot3

启动SpringBoot3,然后访问 我们添加日志的路径: http://localhost:8080/

  .   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v3.2.3)15:59:10.491 INFO  com.ts.Springboot3LokiApplication  - Starting Springboot3LokiApplication using Java 20.0.1 with PID 15984 (D:\IdeaProjects\springboot3_loki\target\classes started by Somken in D:\IdeaProjects\springboot3_loki)
15:59:10.498 INFO  com.ts.Springboot3LokiApplication  - No active profile set, falling back to 1 default profile: "default"
15:59:11.243 INFO  o.s.b.w.e.tomcat.TomcatWebServer  - Tomcat initialized with port 8080 (http)
15:59:11.252 INFO  o.a.coyote.http11.Http11NioProtocol  - Initializing ProtocolHandler ["http-nio-8080"]
15:59:11.254 INFO  o.a.catalina.core.StandardService  - Starting service [Tomcat]
15:59:11.254 INFO  o.a.catalina.core.StandardEngine  - Starting Servlet engine: [Apache Tomcat/10.1.19]
15:59:11.298 INFO  o.a.c.c.C.[Tomcat].[localhost].[/]  - Initializing Spring embedded WebApplicationContext
15:59:11.299 INFO  o.s.b.w.s.c.ServletWebServerApplicationContext  - Root WebApplicationContext: initialization completed in 757 ms
15:59:11.618 INFO  o.a.coyote.http11.Http11NioProtocol  - Starting ProtocolHandler ["http-nio-8080"]
15:59:11.629 INFO  o.s.b.w.e.tomcat.TomcatWebServer  - Tomcat started on port 8080 (http) with context path ''
15:59:11.637 INFO  com.ts.Springboot3LokiApplication  - Started Springboot3LokiApplication in 1.912 seconds (process running for 2.551)
15:59:54.368 INFO  o.a.c.c.C.[Tomcat].[localhost].[/]  - Initializing Spring DispatcherServlet 'dispatcherServlet'
15:59:54.369 INFO  o.s.web.servlet.DispatcherServlet  - Initializing Servlet 'dispatcherServlet'
15:59:54.370 INFO  o.s.web.servlet.DispatcherServlet  - Completed initialization in 1 ms
15:59:54.405 INFO  com.ts.controller.MainController  - 这是一条info信息消息
15:59:54.405 ERROR com.ts.controller.MainController  - This is an error message

提示:只有访问过了这个方法,才能产生日志

grafana_245">3、在grafana中查看日志

grafana_249">3.1、登录grafana

打开grafana:http://172.23.1.235:3000/
在这里插入图片描述

登录进去以后界面如下
在这里插入图片描述

3.2、查询日志

点击左侧的 ”explore“ 探索 按钮
在这里插入图片描述
在这里插入图片描述

3.3、查询我们的SpringBoot发送过来的日志

在这里插入图片描述

3.4、按日志级别查询

在这里插入图片描述

3.5、按主机查询

在这里插入图片描述


http://www.ppmy.cn/server/124716.html

相关文章

lua基础语法

Lua 是一种轻量级的脚本语言&#xff0c;它以其简洁和灵活性而闻名。以下是 Lua 基础语法的一些关键点&#xff1a; 1. 变量声明 Lua 中的变量声明需要使用 local 关键字&#xff0c;表示变量的作用域仅限于当前区块。 local x 10 -- 局部变量 x 20 -- 全局变量&a…

uniapp 动态修改input样式

最近在用HBuilderx工具开发蓝牙调试工具&#xff0c;项目采用uniapp、vue3.0架构&#xff0c;需求设计为在向蓝牙模块发送数据之前&#xff0c;监测input是否为空&#xff0c;如果为空&#xff0c;则input边框橙红色。界面如下图所示&#xff1a; uniapp架构采用 .vue格式文件&…

uniapp路由跳转

一、uni.navigateTo(object) 保留当前页面&#xff0c;跳转到指定非tabBar页面&#xff0c;如&#xff1a; uni.navigateTo({url:/pages/demo1/demo1, }) 二、uni.redirectTo(object) 关闭当前页面&#xff0c;跳转到指定非tabBar页面&#xff0c;如&#xff1a; uni.redi…

19 基于51单片机的倒计时音乐播放系统设计

目录 一、主要功能 二、硬件资源 三、程序编程 四、实现现象 一、主要功能 五个按键&#xff0c;分别为启动按键&#xff0c;则LCD1602显示倒计时&#xff0c;音乐播放 设置按键&#xff0c;可以设置倒计时的分秒&#xff0c;然后加减按键&#xff0c;还有最后一个暂停音乐…

CICD从无到会

一 CICD是什么 CI/CD 是指持续集成&#xff08;Continuous Integration&#xff09;和持续部署&#xff08;Continuous Deployment&#xff09;或持续交付&#xff08;Continuous Delivery&#xff09; 1.1 持续集成&#xff08;Continuous Integration&#xff09; 持续集成是…

大模型+AIGC技术实操:GPT 大模型部署使用 AIGC实战落地方案

一、GPT大模型简介 GPT是基于Transformer架构的预训练语言模型&#xff0c;通过大规模的无监督学习&#xff0c;它已展现出了惊人的文本生成和理解能力。部署GPT大模型意味着将其融入到实际应用中&#xff0c;如智能写作、客服对话、内容推荐等领域。 二、GPT大模型部署步骤 …

Redisearch 入门指南构建高性能搜索应用

1. 概述 Redisearch 是一个强大的全文搜索引擎&#xff0c;基于流行的 Redis 数据库构建&#xff0c;专为高效的数据检索而设计。它结合了 Redis 的快速存储能力和搜索引擎的复杂查询功能&#xff0c;使得开发者能够在海量数据中实现实时搜索体验。Redisearch 支持丰富的特性&…

web前端与koa框架node后端实现分片断点上传

web前端,先选择文件,然后点击上传 html代码如下: <div><input type="file" /><el-button @click="uploadFile()" type="primary">上传</el-button> </div> 上传代码如下 其实也就是每次传50mb,如果网络突然…