日志还是使用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. P4101 [HEOI2014]人人尽说江南好

    题目描述 小 Z 是一个不折不扣的 ZRP(Zealot Round-game Player,回合制游戏狂热玩家),最近他 想起了小时候在江南玩过的一个游戏. 在过去,人们是要边玩游戏边填词的,比如这 ...

  2. Java参数引用传递之例外:null

    今天写链表的时候写了一个函数,实参是一个空链表,应该是按引用传参,但是在函数内修改了链表,外部的链表没有变化. 原来是null作为参数传递的时候,就不是引用传参了. 引自:http://blog.cs ...

  3. 用vim去掉utf-8 BOM

    '去掉utf-8 BOM :set nobomb '保留utf-8 BOM :set bomb

  4. [CF1110H]Modest Substrings

    description CodeForces 定义一个正整数\(x\)是合适的当且仅当\(l\le x\le r\),其中\(l,r\le 10^{800}\). 找到一个长度为\(n\)的数字串,使 ...

  5. poj2761 feed the dog

    题目链接:http://poj.org/problem?id=2761 Description Wind loves pretty dogs very much, and she has n pet ...

  6. CF1009F Dominant Indices 解题报告

    CF1009F Dominant Indices 题意简述 给出一颗以\(1\)为跟的有根树,定义\(d_{i,j}\)为以\(i\)为根节点的子树中到\(i\)的距离恰好为\(j\)的点的个数,对每 ...

  7. BGP的那些安全痛点(转)

    0x00 BGP(RFC 1771. RFC 4271)定义 全称是Border Gateway Protocol, 对应中文是边界网关协议,最新版本是BGPv4. BGP是互联网上一个核心的互联网去 ...

  8. redis动态扩展内存

    需求:将redis内存从1G扩展到3G,不中断服务 1.打开客户端 # redis-cli -p 6391 2.查看当前值 redis 127.0.0.1:6391> config get ma ...

  9. 正确理解 LEAL (Load Effective Address) 指令

    LEAL: leal S, D    ->    D ← &S 在 CSAPP (Computer Systems: A Programmer’s Perspective) 中,对 LE ...

  10. C++继承与组合的区别

    C++程序开发中,设计孤立的类比较容易,设计相互关联的类却比较难,这其中会涉及到两个概念,一个是继承(Inheritance),一个是组合(Composition).因为二者有一定的相似性,往往令程序 ...