使用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 ...
随机推荐
- ie8 ajaxSubmit 上传文件提示下载
转载 解决ie下ajaxsubmit上传文件提示下载文件问题 主要是应为放回类型为json,返回text/html
- 卸载Visual Studio最佳方法难道真的是重装系统?
卸载Visual Studio最佳方法难道真的是重装系统? 卸载Visual Studio最佳方法难道真的是重装系统? 使用TotalUninstaller貌似也没有效果,默认卸载的,程序列表里面还是 ...
- C++解析命令行参数(仿C语言args)
说好不造轮子的,就管不住这手 #include <cstdio> #include <string> #include <vector> bool ParseCha ...
- python参数传递方式
原文地址:http://www.cnblogs.com/zhaopengcheng/p/5492183.html python中一切皆对象,函数中参数传递的是对象的引用. 1在函数中改变变量指向的对象 ...
- 【刷题】BZOJ 2243 [SDOI2011]染色
Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段), 如 ...
- Android学习笔记——关于onConfigurationChanged(转)
从事Android开发,免不了会在应用里嵌入一些广告SDK,在嵌入了众多SDK后,发现几乎每个要求在AndroidManifest.xml申明Activity的广告SDK都会要求加上注明这么一句属性: ...
- Jenkins(四)---Jenkins添加密钥对
一.添加密钥 1.添加git用户和git密码对 ,用于git客户端从gitlab上拉取代码到本地 /** lihaibo 文章内容都是根据自己工作情况实践得出. *版权声明:本博客欢迎转发,但请保留原 ...
- Activity工作流(2)-入门安装运行第一个例子
转: Activity工作流(2)-入门安装运行第一个例子 置顶 2017年05月24日 15:58:50 li_ch_ch 阅读数:24432 版权声明:本文为博主原创文章,未经博主允许不得转载 ...
- 【转】基于R语言构建的电影评分预测模型
一,前提准备 1.R语言包:ggplot2包(绘图),recommenderlab包,reshape包(数据处理) 2.获取数据:大家可以在明尼苏达州大学的社会化计算研 ...
- Python操作sqlserver 2000
在网上找到了pyodbc这个模块,本人操作系统是 Ubuntu 16.04 , Python 2.7 已经安装了pip 按照 官方文档 的步骤成功安装. 但是需要跨平台. 使用pyodbc在wind ...