0216 aop和打印数据库执行日志
需求

maven依赖

<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.8.7</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
打印sql
配置要点:
- 驱动配置 application.properties
spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
spring.datasource.url=jdbc:p6spy:mysql://localhost:3306/demo_datasource
- psy配置
# 单行日志
logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat
# 使用Slf4J记录sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准,单位秒
outagedetectioninterval=2
aop打印持久层执行时间
使用aop实现;
package com.springbootpractice.demo.p6spy.aop;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
/**
* 说明:aop配置
* @author carter
* 创建时间: 2020年02月16日 8:49 下午
**/
@Aspect
@Component
@Slf4j
public class PrintTimeCostAspectJConfig {
@SneakyThrows
@Around("myPointCut()")
public Object around(ProceedingJoinPoint pj) {
Object res = null;
String methodName = pj.getSignature().toShortString();
StopWatch stopWatch = new StopWatch(methodName);
stopWatch.start();
try {
res = pj.proceed();
} catch (Throwable ex) {
throw ex;
} finally {
stopWatch.stop();
log.warn("{}执行耗时{}毫秒", methodName, stopWatch.getTotalTimeMillis());
}
return res;
}
@Pointcut("execution(* com.springbootpractice.demo.p6spy.web..*(..))")
public void myPointCut() {
}
}
启用aop注解:
@EnableAspectJAutoProxy(proxyTargetClass = true)
小结
来个效果截图:
通过本片文章,你可以学会:
- 给代码添加aop切面,增加日志或者打印出方法执行总耗时;
- 给你的数据持久层打印出所有的sql语句,方便生产环境排查问题;
希望大家平安度过冠疫!每天持续精进!

原创不易,转载请注明出处。
0216 aop和打印数据库执行日志的更多相关文章
- Spring Boot 中使用自定义注解,AOP 切面打印出入参日志及Dubbo链路追踪透传traceId
一.使用背景 开发排查系统问题用得最多的手段就是查看系统日志,在分布式环境中一般使用 ELK 来统一收集日志,但是在并发大时使用日志定位问题还是比较麻烦,由于大量的其他用户/其他线程的日志也一起输出穿 ...
- 在EF6.0中打印数据库操作日志
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- log4j+mybatis打印数据库日志
参考文献:一:http://blog.csdn.net/rangqiwei/article/details/50825090 二:http://www.mybatis.org/mybatis-3/zh ...
- 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- Selenium WebDriver Log4j打印执行日志
在自动化测试脚本的执行过程中,使用log4j在日志文件中打印执行日志,用于监控和后续调试脚本. Log4j.xml 文件 <log4j:configuration xmlns:log4j=&qu ...
- 46. Spring Boot中使用AOP统一处理Web请求日志
在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...
- spring AOP自定义注解方式实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- SpringBoot2.0 使用AOP统一处理Web请求日志(完整版)
一,加入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- canal 基于Mysql数据库增量日志解析
canal 基于Mysql数据库增量日志解析 1.前言 最近太多事情 工作的事情,以及终身大事等等 耽误更新,由于最近做项目需要同步监听 未来电视 mysql的变更了解到公司会用canal做增量监 ...
随机推荐
- Azure Media Services -可提供视频点播(VOD)
Azure Media Services 提供直播/VOD点播相关的功能. •提供编码和打包以适用于各种设备播放视频(IOS/Android/web等). •向大量在线观众流式传输实时直播,例如活动 ...
- MySQL快速回顾:数据库和表操作
前提要述:参考书籍<MySQL必知必会> 利用空闲时间快速回顾一些数据库基础. 4.1 连接 在最初安装MySQL,可能会要求你输入一个管理登录(通常为root)和一个口令(密码). 连接 ...
- Tomcat8.5安装与配置的坑
本文只是单纯记录一下tomcat配置的坑! 1.下载官网:https://tomcat.apache.org/下载后解压到根目录,盘符任意.但必须有jdk,本人用的是jdk1.8 2.配置环境变量在c ...
- excel的count、countif、sunif、if
一.count统计数值个数 格式:count(指定区域) , 例如:count(B2:G5) 二.countif统计数值满足条件个数 格式:COUNTIF(条件区域,指定条件) ,例如:count ...
- Can you answer these queries? HDU - 4027 有点坑
#include<iostream> #include<cstring> #include<cstdio> #include<math.h> #incl ...
- 旷视向左、商汤向右,AI一哥之名将落谁家
编辑 | 于斌 出品 | 于见(mpyujian) AI风口历经多年洗礼之后,真正意义上的AI第一股终于要来了. 相比于聚焦在语音识别技术上的科大讯飞.立足互联网产业的百度.发力人形机器人领域的优必选 ...
- SpringCloud(一)
什么是SpringCloud? Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...
- windows定时重启
先准备好脚本restart.bat 新建一个txt,写入shutdown shutdown -s -t 10 十秒后重启,更改后缀为.bat批处理文件,切记编辑好后缀千万不要直接点开,否则就会直接调用 ...
- Tomcat开启JMX监控
搭建模拟环境: 操作系统:centos7内存:1Gjdk:1.8.0_131tomcat:8.0.48 环境准备我们这里就不直接演示了,直接配置tomcat的jmx 1.进入到tomcat的bin目录 ...
- 剑指offer-面试题29-顺时针打印矩阵-矩阵
/* 题目: 输入一个矩阵,按照从外到内顺时针的顺序依次打印每一个数字. */ /* 思路: 1.将打印矩阵看作是打印一个个从外向内的环. 2.每一个环都有一个起始节点,起始节点的坐标*2小于行数和列 ...