日志还是使用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日志的更多相关文章

  1. Spring之事件监听(观察者模型)

    目录 Spring事件监听 一.事件监听案例 1.事件类 2.事件监听类 3.事件发布者 4.配置文件中注册 5.测试 二.Spring中事件监听分析 1. Spring中事件监听的结构 2. 核心角 ...

  2. spring中配置监听队列的MQ

    一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置.  <bean id="axx" ...

  3. Spring的事件监听机制

    最近公司在重构广告系统,其中核心的打包功能由广告系统调用,即对apk打包的调用和打包完成之后的回调,需要提供相应的接口给广告系统.因此,为了将apk打包的核心流程和对接广告系统的业务解耦,利用了spr ...

  4. 十一、Spring之事件监听

    Spring之事件监听 ApplicationListener ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成Applica ...

  5. Windows平台下Oracle监听服务启动过程中日志输出

    Windows平台下Oracle监听服务启动过程中日志输出记录. 日志目录:D:\app\Administrator\diag\tnslsnr\WIN-RU03CB21QGA\listener\tra ...

  6. 利用spring的ApplicationListener监听某一类事件的发生

    1.ApplicationListener在使用过程中可以监听某一事件的发生,可以做出相应的处理,这个方式不常用,但是在特殊情况下面还是有用的. 2.导包pom.xml <project xml ...

  7. Spring boot实现监听Redis key失效事件实现和其它方式

    需求: 处理订单过期自动取消,比如下单30分钟未支付自动更改订单状态 用户绑定隐私号码当订单结束取消绑定等 解决方案1: 可以利用redis自带的key自动过期机制,下单时将订单id写入redis,过 ...

  8. Spring的事件监听ApplicationListener

    ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制. 如果容器中存在Applica ...

  9. Spring Cloud Stream监听已存在的Queues/Exchanges

    环境准备 rabbitmq已运行,端口5672,控制台web端口15672,用户名密码guest/guest 引入spring cloud stream依赖 compile('org.springfr ...

随机推荐

  1. 【bzoj5210】最大连通子块和 树链剖分+线段树+可删除堆维护树形动态dp

    题目描述 给出一棵n个点.以1为根的有根树,点有点权.要求支持如下两种操作: M x y:将点x的点权改为y: Q x:求以x为根的子树的最大连通子块和. 其中,一棵子树的最大连通子块和指的是:该子树 ...

  2. 02.基于IDEA+Spring+Maven搭建测试项目--详细过程

    一.背景介绍 1.1公司相关技术 Git:是一款免费的开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,方便多人集成开发 Maven:是基于项目对象模型(POM),可以通过一小段描述信息 ...

  3. 013 BIO、NIO、AIO的区别

    作者:nnngu GitHub:https://github.com/nnngu 博客园:http://www.cnblogs.com/nnngu 简书:https://www.jianshu.com ...

  4. PKUWC Slay The Spire

    题面链接 LOJ sol 好神啊.果然\(dp\)还是做少了,纪录一下现在的思维吧\(QAQ\). 我们首先可以发现期望是骗人的,要不然他乘的是什么xjb玩意. 其实就是要求所有方案的最优方案和. 因 ...

  5. hdu2138 Miller_Rabin

    Description   Give you a lot of positive integers, just to find out how many prime numbers there are ...

  6. BZOJ 2720: [Violet 5]列队春游

    2720: [Violet 5]列队春游 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 189  Solved: 133[Submit][Status] ...

  7. UVA.10325 The Lottery (组合数学 容斥原理 二进制枚举)

    UVA.10325 The Lottery (组合数学 容斥原理) 题意分析 首先给出一个数n,然后给出m个数字(m<=15),在[1-n]之间,依次删除给出m个数字的倍数,求最后在[1-n]之 ...

  8. go语言操作mongodb

    Install the MongoDB Go Driver The MongoDB Go Driver is made up of several packages. If you are just ...

  9. 求n个数中前k个数,按之前的顺序输出(HK IPM)

    For smaller k: #include <cstdio> #include <cstdlib> #include <cstring> #include &l ...

  10. RF - 完整用例展示

    将重复性动作进行封装为keyword,在测试用例中调用keyword. *** Settings *** Documentation Simple example using SeleniumLibr ...