使用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 记录操作日志、异常日志
平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...
随机推荐
- Node版本管理器NVM常用命令
NVM是什么?nvm (Node Version Manager) 是Nodejs版本管理器,可对不同的node版本快速进行切换. 为什么要用NVM?基于node的工具和项目越来越多,但是每个项目使用 ...
- 浅谈js的类数组对象arguments
类数组对象:arguments总所周知,js是一门相当灵活的语言.当我们在js中在调用一个函数的时候,我们经常会给这个函数传递一些参数,js把传入到这个函数的全部参数存储在一个叫做arguments的 ...
- (转)新建maven项目时报错Error:Maven Resources Compiler: Maven project configuration required for module 'XX'解决方法
转载地址:https://blog.csdn.net/qq784515681/article/details/85070195 在新建maven项目时,Problems中报错: Error:Maven ...
- aiohttp_spider
aiohttp_spider_def: import asyncio import re import aiohttp import aiomysql from pyquery import PyQu ...
- oracle 数据库下所有表结构、数据量及缺失值统计
表结构 SELECT t1.TABLE_NAME, t1.COLUMN_NAME, t1.DATA_TYPE || '(' || t1.DATA_LENGTH || ')', t2.COMMENTS ...
- 读取只包含标签的xml
什么是XML XML是可扩展标记语言(Extensible Markup Language)的缩写,其中标记是关键部分.用户可以创建内容,然后使用限定标记标记它,从而使每个单词.短语或块成为可识别.可 ...
- jmeter录制移动端脚本
jmeter录制脚本有两种方式,一种借助外部工具badbody,一种是本身的功能,使用代理服务器,介绍下如何使用代理服务器录制脚本.我一般在测app或者移动端H5页面时才会录制,所以此文也针对移动端. ...
- DIV 自定义滚动条样式(二)
流浏览器自带的滚动条样式很丑,确实有必要美化. 滚动条从外观来看是由两部分组成:1,可以滑动的部分,我们叫它滑块2,滚动条的轨道,即滑块的轨道,一般来说滑块的颜色比轨道的颜色深. 滚动条的css样式主 ...
- Linux学习笔记-第10天 特殊的交换分区
关键词,分区.mkswap swapon .uquota,RAID,/etc/fstab 此章开始,难度有些提升.不过还好自己有点基础.
- 洛谷P1283 平板涂色 &&一本通1445:平板涂色
题目描述 CE数码公司开发了一种名为自动涂色机(APM)的产品.它能用预定的颜色给一块由不同尺寸且互不覆盖的矩形构成的平板涂色. 为了涂色,APM需要使用一组刷子.每个刷子涂一种不同的颜色C.APM拿 ...