使用spring aop 记录接口日志
spring配置文件中增加启用aop的配置
<!-- 增加aop 自动代理配置 -->
<aop:aspectj-autoproxy />
切面类配置
package com.zchx.acvices; import java.text.SimpleDateFormat; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; /**
* 切面日志记录
*
* @version V1.0
* @author songxiaotong
* @date 2018年2月6日 下午2:39:03
* @Description
*/
// 声明这是一个组件
@Component
// 声明这是一个切面Bean
@Aspect
public class Advices { /**
* 日志记录工具
*/
private static final Logger LOGGER = LoggerFactory.getLogger(Advices.class); /**
* 默认构造函数
*/
public Advices() {
LOGGER.debug("初始化日志切面");
} /**
* 配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
* <p>
* 扫描com.sixeco下面的所有类
*
* @see [类、类#方法、类#成员]
*/
@Pointcut("execution(* com.zhichenhaixin..*.*(..)) or execution(* com.zchx..*.*(..))")
public void aspect() {
} /**
* 配置前置通知,使用在方法aspect()上注册的切入点
* <p>
* 同时接受JoinPoint切入点对象,可以没有该参数
*
* @param joinPoint 切入点
* @see [类、类#方法、类#成员]
*/
@Before("aspect()")
public void before(JoinPoint joinPoint) {
// LOGGER.debug("before {}", joinPoint.getSignature().toString());
} /**
* 配置后置通知,使用在方法aspect()上注册的切入点
* <p>
* </p>
*
* @param joinPoint 切入点
* @see [类、类#方法、类#成员]
*/
@After("aspect()")
public void after(JoinPoint joinPoint) {
// LOGGER.debug("after {}", joinPoint.getSignature().toString());
} /**
* 配置环绕通知,使用在方法aspect()上注册的切入点
* <p>
* 记录方法开始到结束的耗时
*
* @param joinPoint 切入点
* @return Object 处理结果
* @throws Throwable 异常
* @see [类、类#方法、类#成员]
*/
@Around("aspect()")
public Object around(JoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
Object object = null;
try {
ProceedingJoinPoint tempJoinPoint = (ProceedingJoinPoint) joinPoint;
object = tempJoinPoint.proceed();
long end = System.currentTimeMillis();
// LOGGER.debug("around {} Use time : {} ms!",
// joinPoint.getSignature().toString(), end - start);
LOGGER.debug("计时时间:{} 耗时:{}毫秒 URI: {} 最大内存: {}m 已分配内存: {}m 已分配内存中的剩余空间: {}m 最大可用内存: {}m",
new SimpleDateFormat("hh:mm:ss.SSS").format(start), end - start,
joinPoint.getSignature().toString(), Runtime.getRuntime().maxMemory() / 1024 / 1024,
Runtime.getRuntime().totalMemory() / 1024 / 1024, Runtime.getRuntime().freeMemory() / 1024 / 1024,
(Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()
+ Runtime.getRuntime().freeMemory()) / 1024 / 1024);
} catch (Throwable e) {
long end = System.currentTimeMillis();
// LOGGER.debug("around {} Use time : {} ms with exception",
// joinPoint.getSignature().toString(), end - start); LOGGER.debug("计时时间:{} 耗时:{}毫秒 URI: {} 最大内存: {}m 已分配内存: {}m 已分配内存中的剩余空间: {}m 最大可用内存: {}m",
new SimpleDateFormat("hh:mm:ss.SSS").format(start), end - start,
joinPoint.getSignature().toString(), Runtime.getRuntime().maxMemory() / 1024 / 1024,
Runtime.getRuntime().totalMemory() / 1024 / 1024, Runtime.getRuntime().freeMemory() / 1024 / 1024,
(Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()
+ Runtime.getRuntime().freeMemory()) / 1024 / 1024); StackTraceElement[] s = e.getStackTrace(); if (s.length >= 1) {
StackTraceElement parentStack = s[0];
LOGGER.error("发生异常 : 类名 >> {}, 函数名 >> {},问题产生行 >> {},类型 >> {}",
new Object[] { parentStack.getClassName(), parentStack.getMethodName(),
parentStack.getLineNumber(), e.getClass().getName() });
}
throw e;
}
return object;
} /**
* <配置后置返回通知,使用在方法aspect()上注册的切入点
* <p>
*
* @param joinPoint 切入点
* @see [类、类#方法、类#成员]
*/
@AfterReturning("aspect()")
public void afterReturn(JoinPoint joinPoint) {
// LOGGER.debug("afterReturn {}", joinPoint.getSignature().toString());
} /**
* 配置抛出异常后通知,使用在方法aspect()上注册的切入点
*
* @param joinPoint 切入点
* @param ex 异常
* @see [类、类#方法、类#成员]
*/
@AfterThrowing(pointcut = "aspect()", throwing = "ex")
public void afterThrow(JoinPoint joinPoint, Exception ex) {
// LOGGER.debug("afterThrow {}", joinPoint.getSignature().toString());
}
}
使用spring aop 记录接口日志的更多相关文章
- Spring aop 记录操作日志 Aspect
前几天做系统日志记录的功能,一个操作调一次记录方法,每次还得去收集参数等等,太尼玛烦了.在程序员的世界里,当你的一个功能重复出现多次,就应该想想肯定有更简单的实现方法.于是果断搜索各种资料,终于搞定了 ...
- Spring aop 记录操作日志 Aspect 自定义注解
时间过的真快,转眼就一年了,没想到随手写的笔记会被这么多人浏览,不想误人子弟,于是整理了一个优化版,在这里感谢智斌哥提供的建议和帮助,话不多说,进入正题 所需jar包 :spring4.3相关联以及a ...
- SpringBoot应用中使用AOP记录接口访问日志
SpringBoot应用中使用AOP记录接口访问日志 本文主要讲述AOP在mall项目中的应用,通过在controller层建一个切面来实现接口访问的统一日志记录. AOP AOP为Aspect Or ...
- Spring Boot中使用AOP记录请求日志
这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...
- SpringBoot 使用AOP记录接口访问日志
文章来源:https://macrozheng.github.io/mall-learning/#/technology/aop_log AOP AOP为Aspect Oriented Program ...
- [编码实践]SpringBoot实战:利用Spring AOP实现操作日志审计管理
设计原则和思路: 元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运营以及审计提供支持 日志记录尽可能减少性能影响 操作描述参数支持动态获取,其他参数自动记录. 1.定义日志记录元注解, ...
- log4j+AOP 记录错误日志信息到文件中
AOP 采用异常通知切入,把指定包的异常记录到日志文件. 先看log4j.properties ,控制台输出的是普通信息, 文件输出的是异常信息. log4j.rootLogger=DEBUG, Co ...
- springboot 2.x整合redis,spring aop实现接口缓存
pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- 使用SpringBoot AOP 记录操作日志、异常日志
平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...
随机推荐
- 3.Python爬虫入门_正则表达式(简单例子)
#2019-11-23 import requests import time import re #Python正则表达式库 if __name__=='__main__': #海量爬取图片数据 # ...
- 201871010116-祁英红《面向对象程序设计(java)》第二周学习总结
博文正文开头格式:(3分) 项目 内容 <面向对象程序设计(java)> https://home.cnblogs.com/u/nwnu-daizh/ 这个作业的要求在哪里 https:/ ...
- Centos7防火墙添加端口
添加 firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效) 重新载入 fire ...
- 了解css
css指层叠样式表(Cascading Style Sheets) 1.样式定义如何显示HTML元素 2.样式通常存储在样式表中 把样式添加到HTML4.0中,是为了解决内容与表现分离的问题 外部样式 ...
- 05-01 seaborn
1.Seaborn 在上节中我们学习了matplotlib,这节课我们来看看另一个可视化的模块seaborn,它是基于matplotlib的更高级的开源库,主要用作于数据可视化,解决了matplotl ...
- 数据仓库010 - MySQL查看所有存储过程,函数,视图,触发器
.查询数据库中的存储过程和函数 方法一: select `name` from mysql.proc where db = 'your_db_name' and `type` = 'PROCEDURE ...
- java.sql.SQLException: Could not establish connection to 192.168.8.111:10000/default: java.net.ConnectException: Connection refused: connect at org.apache.hadoop.hive.jdbc.HiveConnection.<init>(HiveC
java.sql.SQLException: Could not establish connection to 192.168.8.111:10000/default: java.net.Conne ...
- Vue.js 源码分析(二十三) 指令篇 v-show指令详解
v-show的作用是将表达式值转换为布尔值,根据该布尔值的真假来显示/隐藏切换元素,它是通过切换元素的display这个css属性值来实现的,例如: <!DOCTYPE html> < ...
- 如何保障MySQL主从复制关系的稳定性?关键词(新特性、crash-safe)
一 前言 MySQL 主从架构已经被广泛应用,保障主从复制关系的稳定性是大家一直关注的焦点.MySQL 5.6 针对主从复制稳定性提供了新特性: slave 支持 crash-safe.该功能可以解决 ...
- cap理论与分布式事务的解决方案
现在很火的微服务架构所设计的系统是分布式系统.分布式系统有一个著名的CAP理论,即一个分布式系统要同时满足一致性(Consistency).可用性(Availablility)和分区容错(Partit ...