slog-spring-boot1.1.0文档—第一章 快速开始

slog-spring-boot是一个用于快速定位SQL问题的工具,支持SpringMvc和Mybatis环境,能自动集成到SpringBoot项目中。它通过拦截器获取SQL执行日志,并在HTTP响应中添加SQL信息,便于开发和测试阶段排查问题。启用可选的密码模式增强安全性。使用步骤包括引入依赖、配置项目和http请求。在启用slog后,接口返回的JSON将包含执行的SQL详情。
摘要由CSDN通过智能技术生成

一、简介

起因

我们在日常的后端Java开发中,你是否碰到过以下这些场景:

(1)产品经理急匆匆过来,来帮我找下这个页面的XXX数据

(2)测试开发在测试环境,测试时,需要和后端频繁交流使用那些表,用来构造测试数据

(3)数据开发处理数据写入表中,页面访问与预期不符时,数据开发需要知道执行了那些SQL

(4)正式环境运行,接口发生SQL异常、访问缓慢,需要定位SQL

通常的做法是从前端页面获得接口地址,搜索接口地址对应后端项目的执行方法,找到对应Mybatis的Mapper.xml中SQL,如果参数处理复杂,还可能需要人脑过代码或测试模拟执行,这个过程重复而乏味,我们可以用代码让这个过程自动、简单、高效起来,slog-spring-boot的作用就是如此,让我们可以直接在接口响应中获得SQL数据。

 

概念

可支持SQL执行日志获取,基于Springboot项目可自动集成,简单快速定位SQL问题

 

特征

  • 支持SpringMvc、Mybatis环境 — 通过SpringMvc过滤器、Mybaits拦截器获得SQL语句、SQL参数、执行时间
  • 支持Http返回Json — 对返回格式是Json形式的接口,自动添加slog字段,返回执行SQL集合
  • 安全性配置 — 支持不同环境不同模式,模式枚举:无密码模式、关闭、有密码模式
  • Spring Boot项目可快速集成 — 通过@EnableSlog注解开启

 

二、使用

Maven Setting

SpringMvc、Mybatis环境

<dependency>
    <groupId>com.v2hoping</groupId>
    <artifactId>slog</artifactId>
    <version>1.1.0</version>
</dependency>

Spring Boot环境

<dependency>
    <groupId>com.v2hoping</groupId>
    <artifactId>slog-spring-boot-starter</artifactId>
    <version>1.1.0</version>
</dependency>

 

配置

名称类型说明
slog.activetrue/false是否启用,默认不启动
slog.passwordActivetrue/false是否密码模式,默认否
slog.password字符串密码,为空则不使用密码

 

密码模式

密码模式必须通过请求头传递密码

请求头类型说明
Slog-Pwd字符串传递配置的密码

 

示例

第一步:确认环境

确认Maven已经引入SpringMvc、Mybatis、Springboot

第二步:Maven引入jar

<dependencies>
   <dependency>
      <groupId>com.v2hoping</groupId>
      <artifactId>slog-spring-boot-starter</artifactId>
      <version>1.1.0</version>
   </dependency>
</dependencies>

第三步:环境配置

建议测试环境开启,正式环境可以关闭或者选择密码模式开启。

@SpringBootApplication
@EnableSlog
public class ServletInitializer extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(ServletInitializer.class);
        Map<String, Object> defaultProperties = new HashMap<>(1);
        defaultProperties.put("druid.datasource.primary.base.package");
        defaultProperties.put("registerErrorPageFilter",false);
        System.setProperty("spring.devtools.restart.enabled", "false");
        application.setDefaultProperties(defaultProperties);
        application.setWebEnvironment(true);
        application.run(args);
    }
}

无密码模式:

slog.active=true

密码模式:

slog.active=true
slog.passwordActive=true
slog.password=123456

第四步:http请求返回

不添加slog组件时

{
    "codeNum": 0,
    "success": true,
    "value": {
        "rr": [
            {
                "name": "医疗健康",
                "rank": 1
            },
            {
                "name": "企业服务",
                "rank": 2
            },
            {
                "name": "金融",
                "rank": 3
            }
        ],
        "content": "一季度医疗健康、企业服务、金融"
    }
}

添加slog组件后

{
	"codeNum": 0,
	"success": true,
	"slog": [{
			"param": "",
			"sql": "select element_type_id,element_type from ad_ver",
			"time": 3
		},
		{
			"param": "701001(Integer), 1(Integer), 2019(Integer)",
			"sql": "select financing_amount_rank from ad_in where platform_id=? and date_id = ? and year = ?",
			"time": 3
		}
	],
	"value": {
		"rr": [{
				"name": "医疗健康",
				"rank": 1
			},
			{
				"name": "企业服务",
				"rank": 2
			},
			{
				"name": "金融",
				"rank": 3
			}
		],
		"content": "一季度医疗健康、企业服务、金融"
	}
}

 

三、结语

下一章我们会介绍如何实现该工具,开发的思路,以及项目中使用的关键技术点也会简略介绍。

github地址源码:https://github.com/v2hoping/slog-spring-boot

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值