接口日志记录AOP实现-LogAspect
使用spring aop日志记录
所需jar包
pom.xml
<!-- logger begin -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<!-- logger end -->
LogAspect.java
package isa.qa.frame.common; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import com.google.gson.Gson; /**
*
* @ClassName: LogAspect
* @Description: 日志记录AOP实现
* @author shaojian.yu
* @date 2014年11月3日 下午1:51:59
*
*/
@Aspect
public class LogAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private String requestPath = null ; // 请求地址
private String args = null; //方法里的参数
private Map<?,?> inputParamMap = null ; // 传入参数
private Map<String, Object> outputParamMap = null; // 存放输出结果
private long startTimeMillis = 0; // 开始时间
private long endTimeMillis = 0; // 结束时间 /**
*
* @Title:doBeforeInServiceLayer
* @Description: 方法调用前触发
* 记录开始时间
* @author shaojian.yu
* @date 2014年11月2日 下午4:45:53
* @param joinPoint
*/
@Before("execution(* isa.qa..*.controller..*.*(..))")
public void doBeforeInServiceLayer(JoinPoint joinPoint) {
startTimeMillis = System.currentTimeMillis(); // 记录方法开始执行的时间
} /**
*
* @Title:doAfterInServiceLayer
* @Description: 方法调用后触发
* 记录结束时间
* @author shaojian.yu
* @date 2014年11月2日 下午4:46:21
* @param joinPoint
*/
@After("execution(* isa.qa..*.controller..*.*(..))")
public void doAfterInServiceLayer(JoinPoint joinPoint) {
endTimeMillis = System.currentTimeMillis(); // 记录方法执行完成的时间
this.printOptLog();
} /**
*
* @Title:doAround
* @Description: 环绕触发
* @author shaojian.yu
* @date 2014年11月3日 下午1:58:45
* @param pjp
* @return
* @throws Throwable
*/
@Around("execution(* isa.qa..*.controller..*.*(..))")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
/**
* 1.获取request信息
* 2.根据request获取session
* 3.从session中取出登录用户信息
*/
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes)ra;
HttpServletRequest request = sra.getRequest();
// 从session中获取用户信息
/* String loginInfo = (String) session.getAttribute("username");
if(loginInfo != null && !"".equals(loginInfo)){
userName = operLoginModel.getLogin_Name();
}else{
userName = "用户未登录" ;
}*/
// 获取输入参数
inputParamMap = request.getParameterMap();
// 获取请求地址
requestPath = request.getRequestURI(); args = Arrays.toString(pjp.getArgs()); // 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行
outputParamMap = new HashMap<String, Object>();
Object result = pjp.proceed();// result的值就是被拦截方法的返回值
outputParamMap.put("result", result); return result;
} /**
*
* @Title:printOptLog
* @Description: 输出日志
* @author shaojian.yu
* @date 2014年11月2日 下午4:47:09
*/
private void printOptLog() {
Gson gson = new Gson(); // 需要用到google的gson解析包
String optTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTimeMillis);
logger.info(//"\n user:"+userName+
"\n"
+"\n url:"+requestPath
+"\n args:"+args
+"\n param:"+gson.toJson(inputParamMap)+";"
+"\n result:"+gson.toJson(outputParamMap)
+"\n");
}
}
注意 "execution(* isa.qa..*.controller..*.*(..))" 里的配置需要根据自己实际项目配置路径
springMVC配置文件中:spring-servlet.xml
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean class="isa.qa.frame.common.LogAspect" />
最后启动服务,调用服务端接口,如果控制台输出形式为:
url:xxx
args:xxx
param:xxx
result:xxx
则aop日志记录正确。。。
接口日志记录AOP实现-LogAspect的更多相关文章
- python添加fluent日志记录-aop
python添加fluent日志,aop实现 1.配置fluent相关信息 fluent_config.ini fluent_config.ini [fluent.aop] #is support f ...
- Spring 接口日志 AOP
接口日志记录AOP实现-LogAspect - 91博客it技术开发者 - 博客园https://www.cnblogs.com/007sx/p/5810818.html Spring AOP(一) ...
- Java学习之系统高可用性渲染接口日志自动服务降级
背景:公司都追求系统的高可用性,这里不可用时间就是其中很重要的一个指标,为此在做系统功能升级迭代的过程中如何快速处理异常恢复正常功能极为重要.现在对新增模块的要求是都增加开关,方便快速关闭异常模块,但 ...
- Spring Boot AOP 扫盲,实现接口访问的统一日志记录
AOP 是 Spring 体系中非常重要的两个概念之一(另外一个是 IoC),今天这篇文章就来带大家通过实战的方式,在编程猫 SpringBoot 项目中使用 AOP 技术为 controller 层 ...
- 使用spring aop 记录接口日志
spring配置文件中增加启用aop的配置 <!-- 增加aop 自动代理配置 --> <aop:aspectj-autoproxy /> 切面类配置 package com. ...
- 手把手教你如何优雅的使用Aop记录带参数的复杂Web接口日志
前言 不久前,因为需求的原因,需要实现一个操作日志.几乎每一个接口被调用后,都要记录一条跟这个参数挂钩的特定的日志到数据库.举个例子,就比如禁言操作,日志中需要记录因为什么禁言,被禁言的人的id和各种 ...
- SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Asp ...
- 十:SpringBoot-配置AOP切面编程,解决日志记录业务
SpringBoot-配置AOP切面编程,解决日志记录业务 1.AOP切面编程 1.1 AOP编程特点 1.2 AOP中术语和图解 2.SpringBoot整合AOP 2.1 核心依赖 2.2 编写日 ...
- 利用AOP与ToStringBuilder简化日志记录
刚学spring的时候书上就强调spring的核心就是ioc和aop blablabla...... IOC到处都能看到...AOP么刚开始接触的时候使用在声明式事务上面..当时书上还提到一个用到ao ...
随机推荐
- Oracle学习笔记之六(DDL:表、索引、视图、同义词、序列操作相关SQL)
下面这些基本的SQL语句应该熟悉,能够灵活运用.最好在不查资料的情况下,能够写出如下的任何代码. 1. 数据表操作相关 --创建表 create table STUDENTS( STUNO ) not ...
- 【C语言】给一组组数,仅仅有两个数仅仅出现了一次,其它全部数都是成对出现的,找出这两个数。
//给⼀组组数,仅仅有两个数仅仅出现了一次.其它全部数都是成对出现的,找出这两个数. #include <stdio.h> int find_one_pos(int num) //找一个为 ...
- vim设置文本宽度
'textwidth' 'tw' number (default 0) local to buffer ...
- 用js实现预览待上传的本地图片
js实现预览待上传的本地图片,代码如下: <form name="form5" id="form5" method="post" ac ...
- Windows下对postgre开启远程连接权限
编辑 删除 前言:Windows下对postgre开启远程连接权限,下面是实际操作过程中的手顺 1.找到postgresql.conf文件,注意安装路径 D:\Program Files (x86)\ ...
- php下载图片到本地
写了一天,就写了这么点代码,凑合用吧. #saveImage.php<?php /** * 图片下载方法,提供两种图片保存方式: * 1.按照图片自带的名称保存 * 2.按照自定义文件名保存 * ...
- Qt学习之路3---Qt中的坐标系统
-Qt使用统一的坐标系统定位窗口部件和位置大小 -Qt部件类提供成员函数在坐标系统中进行定位 -QWidget类提供了窗口部件所需的坐标系统成员函数 代码测试 #include "widge ...
- How To Install and Configure Elasticsearch on Ubuntu 14.04
Reference: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-elasticsear ...
- MySQL 日期计算
MySQL提供了几个函数,可以用来计算日期,常用的例子就是,计算年龄或提取日期部分. 1. 计算年龄: mysql中要想计算一个人的年龄,相当于当前日期的年和出生日期之间的差.如果当前日期的日历年比出 ...
- 完成wamp安装后Mysql配置记录
注意:下面如没有特殊说明,默认都是修改my.ini配置,修改完配置需要重启mysql服务. 1.安装完wamp后输入命令有乱码提示: 运行mysql命令时,出现的错误提示是乱码 : ERROR ...