需求

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

配置要点:

  1. 驱动配置 application.properties

spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
spring.datasource.url=jdbc:p6spy:mysql://localhost:3306/demo_datasource
  1. 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)

小结

来个效果截图:

通过本片文章,你可以学会:

  1. 给代码添加aop切面,增加日志或者打印出方法执行总耗时;
  2. 给你的数据持久层打印出所有的sql语句,方便生产环境排查问题;

希望大家平安度过冠疫!每天持续精进!

代码点我!

原创不易,转载请注明出处。

0216 aop和打印数据库执行日志的更多相关文章

  1. Spring Boot 中使用自定义注解,AOP 切面打印出入参日志及Dubbo链路追踪透传traceId

    一.使用背景 开发排查系统问题用得最多的手段就是查看系统日志,在分布式环境中一般使用 ELK 来统一收集日志,但是在并发大时使用日志定位问题还是比较麻烦,由于大量的其他用户/其他线程的日志也一起输出穿 ...

  2. 在EF6.0中打印数据库操作日志

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. log4j+mybatis打印数据库日志

    参考文献:一:http://blog.csdn.net/rangqiwei/article/details/50825090 二:http://www.mybatis.org/mybatis-3/zh ...

  4. 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  5. Selenium WebDriver Log4j打印执行日志

    在自动化测试脚本的执行过程中,使用log4j在日志文件中打印执行日志,用于监控和后续调试脚本. Log4j.xml 文件 <log4j:configuration xmlns:log4j=&qu ...

  6. 46. Spring Boot中使用AOP统一处理Web请求日志

    在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...

  7. spring AOP自定义注解方式实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  8. SpringBoot2.0 使用AOP统一处理Web请求日志(完整版)

    一,加入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  9. canal 基于Mysql数据库增量日志解析

    canal 基于Mysql数据库增量日志解析  1.前言  最近太多事情 工作的事情,以及终身大事等等 耽误更新,由于最近做项目需要同步监听 未来电视 mysql的变更了解到公司会用canal做增量监 ...

随机推荐

  1. Centos中Redis的下载编译与安装(超详细)

    场景 NoSQL,泛指非关系型的数据库,NoSQL即Not-Only SQL,它可以作为关系型数据库的良好补充.随着互联网web2.0网站的兴起,非关系型的数据库现在成了一个极其热门的新领域,非关系数 ...

  2. POSIX简介

    POSIX:Potable Operating System Interface of UNIX (可移植操作系统接口),是IEEE为要在各种UNIX操作系统上运行软件,而定义API的一系列互相关联的 ...

  3. pycharm的这些配置,你都知道吗

    前言 对于一枚pycharm工具的使用新手,正确了解这门工具的配置,在使用过程中遇到的很多问题也可以迎刃而解哦!! 文章篇幅有限,本篇文章提供以下配置手段: 1.字体大小调整 2.显示你需要的工具窗口 ...

  4. centos tomcat解压版安装

    解压: tar -xzvf apache-tomcat-8.5.23.tar.gz -C /usr/local/java 配置Tomcat的环境变量: export CATALINA_HOME=/us ...

  5. Java多线程之线程的协作

    等待队列 每个实例都有一个等待,他是在实例的wait方法执行后停止操作的队列,除非发现以下情况,线程才会退出等待队列 1.有其他线程的notify方法来唤醒线程 2.有其他线程的notifyAll方法 ...

  6. C++ 与String有关的函数!!!

    String函数 1.字符串的输入 (1) string s; cin >> s;//碰到空格等分隔符会终端输入 /* string s; cin >> s;//如果输入 he ...

  7. 02-React基础语法(2)

    一.条件渲染 语法:使用原生 js 的 if 或者 三元表达式 判断   例子:判断用户是否登录,提示:已登录.未登录 (User组件) <script type="text/babe ...

  8. 通过 Chrome浏览器 查看http请求报文

    as we all know  HTTP 请求报文 包含请求行.请求头和请求体三部分 请求行:(请求方式 资源路径 协议/版本) 例如:POST /test/index.html HTTP/1.1 P ...

  9. 嵊州D5T2 折纸 folding

    折纸 folding [问题描述] 在非常紧张的 NOIP 考试中,有人喜欢啃指甲,有人喜欢转铅笔,有人喜欢撕 纸条,……而小 x 喜欢迷折纸. 现有一个 W * H 的矩形纸张,监考老师想知道,小 ...

  10. 0012 基于DRF框架开发(04 序列化器的字段与选项)

    1 常用字段类型 字段 构造方式 BooleanField BooleanField() NullBooleanField NullBooleanField() CharField CharField ...