SpringBoot 统计更多Api接口SQL相关日志信息

统计(查询,更新,批量更新)SQL执行次数及用时并输出log

import com.zhangziwa.practisesvr.utils.log.LogContext;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.stereotype.Component;import java.sql.Statement;@Intercepts({@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),@Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),@Signature(type = StatementHandler.class, method = "batch", args = {Statement.class})})
@Component
public class SqlExecuteInterceptor implements Interceptor {@Overridepublic Object intercept(Invocation invocation) throws Throwable {System.err.println("***SqlExecuteInterceptor.intercept***");long startTime = System.currentTimeMillis();try {return invocation.proceed();} finally {long executionCost = System.currentTimeMillis() - startTime;LogContext.incrementSqlCount();LogContext.incrementSqlCost(executionCost);}}
}

统计查询数据量并输出log

import com.zhangziwa.practisesvr.utils.log.LogContext;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;import java.sql.Statement;
import java.util.Collection;@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})})
@Component
public class SqlReadRowInterceptor implements Interceptor {@Overridepublic Object intercept(Invocation invocation) throws Throwable {Object proceed = invocation.proceed();if (proceed instanceof Collection) {LogContext.incrementSqlSearchedRowCount(((Collection<?>) proceed).size());} else {LogContext.incrementSqlSearchedRowCount(0);}return proceed;}
}

在这里插入图片描述

查询单条数据

[2024-01-24 10:52:31.365_365] [WARN ] [http-nio-8080-exec-3] [LogFilter.java:21][LogFilter.doFilter: Start processing request at 2024-01-24T02:52:31.365178500Z - /students/8]
***LogFilter.doFilter.start***
***RequestHeaderCheckFilter.doFilter.start******ResponsePostInterceptor.preHandle***
***LogInterceptor.preHandle***
[2024-01-24 10:52:31.389_389] [WARN ] [http-nio-8080-exec-3] [LogInterceptor.java:37][LogInterceptor.postHandle: Start processing request at 2024-01-24T02:52:31.389802500Z - /students/8]***StudentController.queryById***
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ab146ea] was not registered for synchronization because synchronization is not active
[2024-01-24 10:52:31.425_425] [INFO ] [http-nio-8080-exec-3] [HikariDataSource.java:110][practisedb - Starting...]
[2024-01-24 10:52:31.526_526] [INFO ] [http-nio-8080-exec-3] [HikariPool.java:565][practisedb - Added connection com.mysql.cj.jdbc.ConnectionImpl@439142cf]
[2024-01-24 10:52:31.528_528] [INFO ] [http-nio-8080-exec-3] [HikariDataSource.java:123][practisedb - Start completed.]
JDBC Connection [HikariProxyConnection@1509777760 wrapping com.mysql.cj.jdbc.ConnectionImpl@439142cf] will not be managed by Spring
==>  Preparing: select id, username, password, age, height, gender, class_id, is_delete from students where id = ?
***SqlExecuteInterceptor.intercept***
==> Parameters: 8(Integer)
<==    Columns: id, username, password, age, height, gender, class_id, is_delete
<==        Row: 8, 汪子韬, lq2fks1eg5, 24, 161.84,, 5, 0
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ab146ea]***ResponsePostAdvice.supports***
***ResponsePostAdvice.beforeBodyWrite******LogInterceptor.postHandle***
***ResponsePostInterceptor.postHandle***
***LogInterceptor.afterCompletion***
[2024-01-24 10:52:34.193_193] [WARN ] [http-nio-8080-exec-3] [LogInterceptor.java:57][LogInterceptor.postHandle: Finished processing request at 2024-01-24T02:52:34.193563200Z - /students/8 in 2804 ms. Status code: 200]
[2024-01-24 10:52:34.965_965] [INFO ] [http-nio-8080-exec-3] [LogInterceptor.java:61][{"traceId":"372050c0aed44614aa5912323cfcc836","end_date":"2024-01-24T10:52:34.9545052+08:00[Asia/Shanghai]","cost":2804,"remoteHost":"0:0:0:0:0:0:0:1","remoteAddr":"0:0:0:0:0:0:0:1","remotePort":5905,"method":"GET","requestURI":"/students/8","status":200,"requestContentLength":-1,"sql_count":1,"sql_cost":367,"sql_searched_row_count":1,"currentThreadTime":62,"currentThreadUserTime":31,"currentThreadAllocatedBytes":24682920}]
[2024-01-24 10:52:34.968_968] [WARN ] [http-nio-8080-exec-3] [LogFilter.java:30][LogFilter.doFilter: Finished processing request at 2024-01-24T02:52:34.968452Z - /students/8 in 3602 ms. Status code: 200]
***ResponsePostInterceptor.afterCompletion******RequestHeaderCheckFilter.doFilter.end***
***LogFilter.doFilter.end***

更新数据

***LogFilter.doFilter.start***
***RequestHeaderCheckFilter.doFilter.start***
[2024-01-24 11:24:34.918_918] [WARN ] [http-nio-8080-exec-1] [LogFilter.java:21][LogFilter.doFilter: Start processing request at 2024-01-24T03:24:34.917981600Z - /students]***ResponsePostInterceptor.preHandle***
***LogInterceptor.preHandle***
[2024-01-24 11:24:34.947_947] [WARN ] [http-nio-8080-exec-1] [LogInterceptor.java:37][LogInterceptor.postHandle: Start processing request at 2024-01-24T03:24:34.947520700Z - /students]***StudentController.edit***
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@207e0209] was not registered for synchronization because synchronization is not active
[2024-01-24 11:24:38.701_701] [INFO ] [http-nio-8080-exec-1] [HikariDataSource.java:110][practisedb - Starting...]
[2024-01-24 11:24:38.831_831] [INFO ] [http-nio-8080-exec-1] [HikariPool.java:565][practisedb - Added connection com.mysql.cj.jdbc.ConnectionImpl@5fb7095]
[2024-01-24 11:24:38.834_834] [INFO ] [http-nio-8080-exec-1] [HikariDataSource.java:123][practisedb - Start completed.]
JDBC Connection [HikariProxyConnection@637086707 wrapping com.mysql.cj.jdbc.ConnectionImpl@5fb7095] will not be managed by Spring
==>  Preparing: update students SET username = ?, password = ?, age = ?, height = ?, gender = ?, is_delete = ? where id = ?***SqlExecuteInterceptor.intercept***
==> Parameters: 杜云(String), RVUY6MDXdu(String), 19(Integer), 173(Integer),(String), false(Boolean), 83(Integer)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@207e0209]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@56251da2] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1108439486 wrapping com.mysql.cj.jdbc.ConnectionImpl@5fb7095] will not be managed by Spring
==>  Preparing: select id, username, password, age, height, gender, class_id, is_delete from students where id = ?***SqlExecuteInterceptor.intercept***
***SqlReadRowInterceptor.intercept***
==> Parameters: 83(Integer)
<==    Columns: id, username, password, age, height, gender, class_id, is_delete
<==        Row: 83, 杜云, RVUY6MDXdu, 19, 173.00,, 5, 0
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@56251da2]***ResponsePostAdvice.supports***
***ResponsePostAdvice.beforeBodyWrite******LogInterceptor.postHandle***
***ResponsePostInterceptor.postHandle***
***LogInterceptor.afterCompletion***
[2024-01-24 11:24:39.549_549] [WARN ] [http-nio-8080-exec-1] [LogInterceptor.java:57][LogInterceptor.postHandle: Finished processing request at 2024-01-24T03:24:39.549151300Z - /students in 4602 ms. Status code: 200]
[2024-01-24 11:24:39.728_728] [INFO ] [http-nio-8080-exec-1] [LogInterceptor.java:61][{"traceId":"1f8722c261494ebf822c8fad242c63be","end_date":"2024-01-24T11:24:39.7205245+08:00[Asia/Shanghai]","cost":4602,"remoteHost":"0:0:0:0:0:0:0:1","remoteAddr":"0:0:0:0:0:0:0:1","remotePort":8335,"method":"PUT","requestURI":"/students","status":200,"requestContentLength":180,"sql_count":2,"sql_cost":338,"sql_searched_row_count":1,"currentThreadTime":125,"currentThreadUserTime":109,"currentThreadAllocatedBytes":30771024}]
[2024-01-24 11:24:39.731_731] [WARN ] [http-nio-8080-exec-1] [LogFilter.java:30][LogFilter.doFilter: Finished processing request at 2024-01-24T03:24:39.731376500Z - /students in 4814 ms. Status code: 200]
***ResponsePostInterceptor.afterCompletion******RequestHeaderCheckFilter.doFilter.end***
***LogFilter.doFilter.end***

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/643844.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

什么是通配监听端口? 什么是通配监听IP?

什么是通配监听端口? 监听端口&#xff1a; 指的是服务器或服务开启的特定TCP或UDP端口号&#xff0c;等待客户端连接或发送数据。TCP/IP协议下每个端口只能由一个服务独占监听&#xff0c;一个服务或应用会指定监听特定的一个或多个端口来接收客户端的连接请求。 例如 Web…

RocketMQ-Windows版本安装

RocketMQ-Windows版本安装 1.环境准备 JDK和maven需要先安装好&#xff0c;我这里使用的JDK1.8版本 Maven 3.8.6版本。需要注意的是&#xff0c;这里配置java时需要指定JAVA_HOME环境变量 RokectMQ才能正常启动。 2.下载RocketMQ 官网下载: https://rocketmq.apache.org/z…

C++读取txt文件中的逐个字符

为了增加读取的灵活性&#xff0c;所以separator和filename都设置为在主函数中获取输入或者在函数中传参的视线方法 举个例子&#xff0c;txt文件如下&#xff1a; household;2;true; 首先声明一个读取数据的文件 void read_data_file(const string& filename,char se…

Matplotlib Mastery: 从基础到高级的数据可视化指南【第30篇—python:数据可视化】

文章目录 Matplotlib: 强大的数据可视化工具1. 基础1.1 安装Matplotlib1.2 创建第一个简单的图表1.3 图表的基本组件&#xff1a;标题、轴标签、图例 2. 常见图表类型2.1 折线图2.2 散点图2.3 条形图2.4 直方图 3. 图表样式与定制3.1 颜色、线型、标记的定制3.2 背景样式与颜色…

CC工具箱使用指南:【属性映射】

一、简介 在规划工作中&#xff0c;经常会遇到这样一种情况&#xff0c;有一组一一对应的值。 比如用地编码和用地名称&#xff0c;用地编码【0101】和用地名称【水田】是对应的。 当你在用地编码字段输入【0101】时&#xff0c;用地名称值就必须为【水田】。 当我们确定用地…

gin路由篇

1. 基本路由 gin 框架中采用的路由库是基于httprouter做的 import ("net/http""github.com/gin-gonic/gin" )func main() {// 1.创建路由r : gin.Default()// 2.绑定路由规则&#xff0c;执行的函数// gin.Context&#xff0c;封装了request和responser.…

Nacos源码下载与运行

早先在linux环境下搭建过nacos环境 即Centos安装部署nacos实战&#xff0c;本次是从官网上下载源码&#xff0c;本地运行看看&#xff0c;记录过程&#xff0c;方便备查。 第一步、Nacos源码下载 推荐到nacos官网下载 Github地址&#xff0c;本次选择最新版&#xff0c;1.4.7…

查看Pytorch的GPU是否可用

查看Pytorch的GPU是否可用 import torch torch.cuda.is_available()返回为True表示 Pytorch 的 GPU 可用&#xff0c;返回为False表示 Pytorch 的 GPU 不可用。 其余命令&#xff1a; # 查看cuda是否可用 torch.cuda.is_available() # 返回当前设备索引 torch.cuda.current_d…

Kubeadm安装单master多node节点K8S集群

kubeadm安装k8s1.25版本集群步骤 环境说明实验环境规划集群搭建规划 初始化安装k8s集群的实验环境安装虚拟机更新yum源和操作系统配置机器主机名配置主机hosts文件&#xff0c;相互之间通过主机名互相访问配置主机之间无密码登录关闭交换分区swap&#xff0c;提升性能修改机器内…

Excel导出警告:文件格式和拓展名不匹配

原因描述&#xff1a; Content-Type 原因&#xff1a;Content-Type&#xff0c;即内容类型&#xff0c;一般是指网页中存在的Content-Type&#xff0c;用于定义网络文件的类型和网页的编码&#xff0c;决定文件接收方将以什么形式、什么编码读取这个文件&#xff0c;这就是经常…

Qt单选按钮

前言 本篇文章介绍Qt的单选按钮&#xff0c;就是QRadioButton QRadioButton是一个选项按钮&#xff0c;可以打开&#xff08;选中&#xff09;或关闭&#xff08;取消选中&#xff09;。单选按钮通常向用户提供“众多之一”的选择。 在一组单选按钮中&#xff0c;一次只能选中…

QT 实现自动生成小学两位数加减法算式

小学生加减法训练 QT实现–自动生成两位数加减法算式&#xff0c;并输出txt文件 可以copy到word文件&#xff0c;设置适当字体大小和行间距&#xff0c;带回家给娃做做题 void MainWindow::test(int answerMax, int count) {// 创建一个随机数生成器QRandomGenerator *gener…

C程序的内存空间布局(栈、堆、数据区、常量区、代码区)

目录 C程序的内存空间布局动态区栈区堆区 静态区数据区常量区代码区 栈的地址测试堆的测试地址静态区演示 橙色 C程序的内存空间布局 补充&#xff1a;内存地址和内存空间 内存地址是一个编号&#xff0c;通常由16进制表示&#xff0c;它代表一个内存空间。在计算机中存储器的…

c语言小游戏之扫雷

目录 一&#xff1a;游戏设计理念及思路 二&#xff1a;初步规划的游戏界面 三&#xff1a;开始扫雷游戏的实现 注&#xff1a;1.创建三个文件&#xff0c;test.c用来测试整个游戏的运行&#xff0c;game.c用来实现扫雷游戏的主体&#xff0c;game.h用来函数声明和包含头文…

机械设计-哈工大课程学习-螺纹连接

圆柱螺纹主要几何参数螺纹参数 ①外径&#xff08;大径&#xff09;&#xff0c;与外螺纹牙顶或内螺纹牙底相重合的假想圆柱体直径。螺纹的公称直径即大径。 ②内径&#xff08;小径&#xff09;&#xff0c;与外螺纹牙底或内螺纹牙顶相重合的假想圆柱体直径。 ③中径&#xff…

解读顺网算力与AI,破局AIGC落地“最后一公里”

全球知名AI科学家吴恩达和李飞飞在CES 2024上预测&#xff0c;2024年将是AI技术继续深化的一年&#xff0c;将成为下一次数字或工业革命真正的变革性驱动力。吴恩达还预测了2024年AI可能的突破性进展&#xff0c;其中包括边缘AI。吴恩达对边缘AI寄予厚望&#xff0c;他认为在笔…

从理论到实践:Dubbo 的 `<dubbo:service>` 与 `<dubbo:reference>` 全面指南

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 从理论到实践&#xff1a;Dubbo 的 与 全面指南 前言<dubbo:service> 和 <dubbo:reference> 基础配置 <dubbo:service>配置<dubbo:reference>服务提供与消费的最佳实践1. 性…

AI-数学-高中-7-函数单调性

原作者视频&#xff1a;函数】5函数单调性&#xff08;易&#xff09;_哔哩哔哩_bilibili 1.什么是单调性&#xff1a; 2.通过画图判断单调性&#xff1a; 3.分段函数单调性&#xff1a; 4.如何利用定义判断单调性&#xff1a; 5.利用单调性解不等式&#xff1a; 5.1 把函数…

鸿蒙应用开发学习:获取手机位置信息

一、前言 移动应用中经常需要获取设备的位置信息&#xff0c;因此在鸿蒙应用开发学习中&#xff0c;如何获取手机的位置信息是必修课。之前我想偷懒从别人那里复制黏贴代码&#xff0c;于是在百度上搜了一下&#xff0c;可能是我输入的关键字不对&#xff0c;结果没有找到想要…

第十三讲_ArkUI栅格布局(GridRow/GrowCol)

ArkUI栅格布局&#xff08;GridRow/GrowCol&#xff09; 1. 栅格布局概述2. GridRow的使用2.1 设置栅格布局的总列数2.2 设置栅格布局的排列方向2.3 设置栅格布局中子组件间距 3. GridCol的使用3.1 设置一个GridCol占栅格布局的列数3.2 设置GridCol在栅格布局中偏移列数3.3 设置…