需求

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. winForm学习 2019年4月11日

    1.Directory静态类 相似类:File.Path.StreamReader.StreamWirter 创建文件夹:Directory.CreateDirectory 删除文件夹:Directo ...

  2. console 打印消息时,可以使用 %c 指定随后的文本样式; %s 可引用参数变量。

    1.console.log 使用 加%c console.log('%c Merry Christmas!!', 'color:green;background:yellow;text-shadow: ...

  3. 常用Content-type对照表

    文件扩展名 Content-type .html text/html .xhtml text/html .gif image/gif .png image/png .jpg image/jpeg 更加 ...

  4. kali linux 渗透入门之基础准备-Burp Suite 代理设置

    一:安装火狐浏览器-插件与设置中文 打开浏览器,复制粘贴这条url: https://addons.mozilla.org/en-US/firefox/addon/chinese-simplified ...

  5. MySQL表名大小写敏感性

    Linux版MySQL 库名与表名是严格区分大小写的: 表的别名是严格区分大小写的: 列名与列的别名在所有的情况下均是忽略大小写的: 变量名也是严格区分大小写的: 修改步骤如下: 1. 编辑[/etc ...

  6. 自定义配置JNOJ

    OJ 全名 online judge 在线判题系统,对于从事编程竞赛的人来说一点都不陌生,今天我们讨论的是怎么样自定义搭建 推荐的开源的OJ有hustOJ,JNOJ 因为hustOJ 是一键安装脚本, ...

  7. [Python] 前程无忧招聘网爬取软件工程职位 网络爬虫 https://www.51job.com

    首先进入该网站的https://www.51job.com/robots.txt页面 给出提示: 找不到该页 File not found 您要查看的页已删除,或已改名,或暂时不可用. 请尝试以下操作 ...

  8. Postman测试上传MultipartFile文件

    单个文件上传 后台代码 //导入excel @PostMapping("/import") public Result excelImport( @RequestParam(&qu ...

  9. 吴裕雄--天生自然 R语言开发学习:模块\包的安装命令

    install.packages('模块包名称') 或者 install.packages('模块包名称',repos='http://cran.us.r-project.org')

  10. 获取指定key对应的node节点信息

    需求:之前写的脚本(https://www.cnblogs.com/imdba/p/10197192.html),每个node上都只有一个slot段范围的情况,本次通过测试,实现了,任意段范围的获取方 ...