使用spring的aop监听所有controller或者action日志
日志还是使用log4,直接配置好文件输出或者控制台打印!
注解或者cml都行,我这里采用xml方式:
spring的配置文件中配置日志类和aop:
<!-- 日志监控类 -->
<bean id="actionLog" class="com.zhuzher.log.ActionLogAspect"></bean>
<!-- 监控所有action -->
<!-- 使用cglib代理 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="logAction" expression="execution(* com.zhuzher.*.action.*.*(..))"/>
<aop:aspect id="b" ref="actionLog">
<!-- <aop:before pointcut-ref="logAction" method="before"/> -->
<aop:after pointcut-ref="logAction" method="after"/>
<!-- <aop:after-returning method="afterReturn" pointcut-ref="logAction" returning="result"/>
<aop:after-throwing method="afterThrow" pointcut-ref="logAction" throwing="ex"/> -->
</aop:aspect>
</aop:config>
,根据需要即可,
然后编写切面类,注意,最好使用cglib代理,需要添加依赖,默认使用jdk代理的话,所代理的类必须有接口,否则报错:
//action日志监听
public class ActionLogAspect { private final static Logger log = Logger.getLogger(ActionLogAspect.class);
/**
* 后置通知(无论方法是否发生异常都会执行,所以访问不到方法的返回值)
*/
public void after(JoinPoint joinPoint)throws IOException{
WriteToLog(joinPoint);
}
//把信息写进日志里面
public void WriteToLog(JoinPoint joinPoint)throws IOException {
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.
getRequestAttributes()).getRequest();
//获取方法名
String cla=joinPoint.getTarget().getClass().getName();//action
String method=joinPoint.getSignature().getName();//method
//获取操作人
HttpSession session = request.getSession();
Manager manager = (Manager) session.getAttribute("SESSION_MANAGER");
Integer userid=null;
String username="";
if(manager!=null){
userid = manager.getId();
username =manager.getUsername();
}
StringBuffer json=new StringBuffer();//获取请求参数
Enumeration<String> names = request.getParameterNames();
while(names.hasMoreElements()){
//获取每一个文本域的name
String name = names.nextElement();
String [] values = request.getParameterValues(name);
if(values!=null && values.length>0){
//输出参数名和参数值
json.append(name+":{");
for(String val:values){
json.append(val+",");
}
if (','==json.charAt(json.length()-1)) json.deleteCharAt(json.length()-1);
json.append("},");
}
}
if (json!=null && json.length()>0 && ','==json.charAt(json.length()-1)) json.deleteCharAt(json.length()-1);
log.debug("{method:{"+cla+"."+method+"}, params:{"+json.toString()+"},user:{id:"+userid+",username:"+username+"}}");
}
}
获取对应的方法名,类名,以及从session中获取当前用户就可以了
使用spring的aop监听所有controller或者action日志的更多相关文章
- Spring之事件监听(观察者模型)
目录 Spring事件监听 一.事件监听案例 1.事件类 2.事件监听类 3.事件发布者 4.配置文件中注册 5.测试 二.Spring中事件监听分析 1. Spring中事件监听的结构 2. 核心角 ...
- spring中配置监听队列的MQ
一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置. <bean id="axx" ...
- Spring的事件监听机制
最近公司在重构广告系统,其中核心的打包功能由广告系统调用,即对apk打包的调用和打包完成之后的回调,需要提供相应的接口给广告系统.因此,为了将apk打包的核心流程和对接广告系统的业务解耦,利用了spr ...
- 十一、Spring之事件监听
Spring之事件监听 ApplicationListener ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成Applica ...
- Windows平台下Oracle监听服务启动过程中日志输出
Windows平台下Oracle监听服务启动过程中日志输出记录. 日志目录:D:\app\Administrator\diag\tnslsnr\WIN-RU03CB21QGA\listener\tra ...
- 利用spring的ApplicationListener监听某一类事件的发生
1.ApplicationListener在使用过程中可以监听某一事件的发生,可以做出相应的处理,这个方式不常用,但是在特殊情况下面还是有用的. 2.导包pom.xml <project xml ...
- Spring boot实现监听Redis key失效事件实现和其它方式
需求: 处理订单过期自动取消,比如下单30分钟未支付自动更改订单状态 用户绑定隐私号码当订单结束取消绑定等 解决方案1: 可以利用redis自带的key自动过期机制,下单时将订单id写入redis,过 ...
- Spring的事件监听ApplicationListener
ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制. 如果容器中存在Applica ...
- Spring Cloud Stream监听已存在的Queues/Exchanges
环境准备 rabbitmq已运行,端口5672,控制台web端口15672,用户名密码guest/guest 引入spring cloud stream依赖 compile('org.springfr ...
随机推荐
- 【译】关于vertical-align你应知道的一切
原文地址:Vertical-Align: All You Need To Know 通常我们需要垂直对齐并排的元素. CSS提供了一些可实现的方法:有时我用浮动float来解决,有时用position ...
- 【版本管理】git本地操作
1.初始化一个Git仓库,使用git init命令. 2.添加文件到Git仓库,分两步: • 第一步,使用命令git add 文件名,注意,可反复多次使用,添加多个文件: • 第二步,使用命令git ...
- T4扩展程序
T4功能强大,不用简直就是浪费青春.vs是没有自带对T4模板编辑的扩展的,写着很累很累(我很low我承认) 这两个驯兽师能帮你驯服它 Devart T4 Editor T4 Toolbox
- centos 7下ldap安装
环境说明: 操作系统:CentOS Linux release 7.5.1804 (Core) LDAP:2.4.44 前提条件: 关闭防火墙.selinux,同时进行时钟同步. 其中XXX需要用域名 ...
- ZJOI2018外省选手酱油记Day1
Day0 上午考试...又爆零了 下午讲完题后放假 然后就滚回去收拾行李准备去\(ZJ\) Day1 衢州?我怎么从来没听过这个地方..肯定是我见识少 下午 上高铁出发,\(3个小时\),看了一下电影 ...
- 【UOJ#80】二分图最大权匹配(KM)
题面 UOJ 题解 模板qaq #include<iostream> #include<cstdio> #include<cstdlib> #include< ...
- 【模板】网络流-最大流模板(Dinic)
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> u ...
- 洛谷 P3205 [HNOI2010]合唱队 解题报告
P3205 [HNOI2010]合唱队 题目描述 为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个人的身高为 ...
- 面试自我介绍之English
Version 1 Hello, everyone. I am so glad to stand here. First of all, I will introduce myself. My nam ...
- CF17E Palisection——优秀的综合计数题
题意翻译 给定一个长度为n的小写字母串.问你有多少对相交的回文子 串(包含也算相交) . 输入格式 第一行是字符串长度n(1<=n<=2*10^6),第二行字符串 输出格式 相交的回文子串 ...