spring aop实现log 日志跟踪
之前写的那篇是基于springboot的(https://www.cnblogs.com/yaoyuan2/p/10302802.html),由于遗留项目用的是spring,因此需要在spring基础上实现。
代码结构

web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-application.xml
</param-value>
</context-param>
...
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- <init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param> --> <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spring-application.xml
<context:component-scan base-package="com.ebc.config" />
因为下边com.ebc.config.LogAspectConfig用到了
@Aspect
@Component
所以,为了扫描到才加以上
spring-servlet.xml
<beans
...
xmlns:aop="http://www.springframework.org/schema/aop" http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
">
<!-- 启动对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy/>
com.ebc.config.LogAspectConfig
package com.ebc.config; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.stereotype.Component; import cn.hutool.core.util.IdUtil; @Aspect
@Component
public class LogAspectConfig {
/**
* 第1个* 方法属性 public/private/....
* 接着,连着的2个*,包
* 然后的1个*,类
* 最后1个*,方法
* (..)方法里的参数。..标示任意参数
*/
@Pointcut("execution(* com.ebc.**.*.*(..))")
public void methodCut(){}
/**
* 方法调用之前调用
*/
@Before("methodCut()")
public void doBefore(JoinPoint joinPoint) throws InterruptedException{
String requestId = String.valueOf(IdUtil.objectId());
MDC.put("requestId",requestId);
} /**
* 方法之后调用
*/
@After("methodCut()")
public void doAfter(JoinPoint joinPoint) {
MDC.clear();
} }
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<CONSOLE name="CONSOLE">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{requestId}] %-5level | %C.%M:%L - %m%n"/>
</CONSOLE>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="CONSOLE"/>
</Root>
<logger name="com.ebc.web" level="INFO" />
<logger name="com.ebc.disruptor" level="INFO" />
</Loggers>
</Configuration>
测试输出:
2019-01-25 10:29:10.609 [5c4a7476cbb0db4b262e20cf] INFO | com.ebc.web.IndexController.index:25 - 接收参数:name=遥远2,minernum=222
一月 25, 2019 10:29:10 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@105fac24: startup date [Fri Jan 25 10:29:10 GMT+08:00 2019]; root of context hierarchy
2019-01-25 10:29:10.625 [5c4a7476cbb0db4b262e20cf] INFO | com.ebc.disruptor.UserMinerProducer.publish:16 - 接收:5c4a7476cbb0db4b262e20cf,222
2019-01-25 10:29:10.635 [] INFO | com.ebc.disruptor.UserMinerHandler.onEvent:18 - userName=5c4a7476cbb0db4b262e20cf,amount=222
mdc的本质采用ThreadLocal,一个线程(含子线程)内共享变量。
而现在的项目用的是disruptor,跨主线程,因此在UserMinerHandler(消费端)无法获取到共享变量。采用了一个变通的方法,就是业务代码传递过去。所以,userName里才填充了MDC的值。
如果有好的方法,欢迎留言。
spring aop实现log 日志跟踪的更多相关文章
- Spring aop 记录操作日志 Aspect
前几天做系统日志记录的功能,一个操作调一次记录方法,每次还得去收集参数等等,太尼玛烦了.在程序员的世界里,当你的一个功能重复出现多次,就应该想想肯定有更简单的实现方法.于是果断搜索各种资料,终于搞定了 ...
- [编码实践]SpringBoot实战:利用Spring AOP实现操作日志审计管理
设计原则和思路: 元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运营以及审计提供支持 日志记录尽可能减少性能影响 操作描述参数支持动态获取,其他参数自动记录. 1.定义日志记录元注解, ...
- Spring AOP实现统一日志输出
目的: 统一日志输出格式 思路: 1.针对不同的调用场景定义不同的注解,目前想的是接口层和服务层. 2.我设想的接口层和服务层的区别在于: (1)接口层可以打印客户端IP,而服务层不需要 (2)接口层 ...
- spring cloud 微服务日志跟踪 sleuth logback elk 整合
看过我之前的文章的就可以一步一步搭建起日志传输到搜索引擎 不知道的 看下之前的文章 (1) 记一次logback传输日志到logstash根据自定义设置动态创建ElasticSearch索引 (2)关 ...
- Spring框架之log日志的使用
1.Spring框架也需要引入日志相关的jar包 * 在spring-framework-3.0.2.RELEASE-dependencies/org.apache.commons/com.sprin ...
- 使用spring aop 记录接口日志
spring配置文件中增加启用aop的配置 <!-- 增加aop 自动代理配置 --> <aop:aspectj-autoproxy /> 切面类配置 package com. ...
- Spring aop 记录操作日志 Aspect 自定义注解
时间过的真快,转眼就一年了,没想到随手写的笔记会被这么多人浏览,不想误人子弟,于是整理了一个优化版,在这里感谢智斌哥提供的建议和帮助,话不多说,进入正题 所需jar包 :spring4.3相关联以及a ...
- spring aop 方法增加日志记录
使用场景: 1:调用外部接口时需要记录出参和入参. 2:分布式系统之间,调用各个系统之间需要记录日志,一旦出现了问题也可以找得到元数据 一言不合,上代码: # 枚举类 package xxxxxxxx ...
- spring aop 实现controller 日志
@Aspect @Component @Slf4j public class ControllerAspact { @Pointcut("execution(public * com.exa ...
随机推荐
- 按钮控件JButton的使用
---------------siwuxie095 工程名:TestUI 包名:com.siwuxie095.ui 类名:TestButton. ...
- 阿里云、宝塔、wordpress建站
1 阿里云 购买一个学生机就行啦 2 宝塔 2.1 更改阿里云的镜像 技巧01:先关掉阿里云之前的镜像 技巧02:到镜像市场中寻找宝塔的镜像资源 2.2 配置安全组 宝塔的控制面板需要开通端口 888 ...
- wpf仿qq边缘自动停靠,支持多屏
wpf完全模仿qq边缘自动隐藏功能,采用鼠标钩子获取鼠标当前状态,在通过当前鼠标的位置和点击状态来计算是否需要隐藏. 以下是实现的具体方法: 一.鼠标钩子实时获取当前鼠标的位置和点击状态 /// &l ...
- ARC100D Equal Cut
传送门 分析 首先我们想到的肯定是n^3暴力枚举,但这显然不行.然后我们想到的就是二分了,但这题没有什么单调性,所以二分也不行.这时候我就想到了先枚举找出p2的位置再在它的左右两边找到p1和p3,但是 ...
- Entity Framework Tutorial Basics(31):Migration from EF 4.X
Migration from Entity Framework 4.1/4.3 to Entity Framework 5.0/6.0 To migrate your existing Entity ...
- CSS相关知识和经验的碎片化记录
1.子DIV块中设置margin-top时影响父DIV块位置的问题 解决办法1:若子DIV块中使用margin-top,则在父DIV块中添加:overflow:hidden; 解决办法2:在子DIV块 ...
- 数据结构 nxd(顺序对)
数据结构 nxd(顺序对) 问题描述 给定 n 个数 a1,a2,...,an,求满足条件的(i,j)数量: i<j 且 a[i]<a[j] ★数据输入输入第一行为一个正整数 n.第二行为 ...
- 数据结构 i_love(我喜欢)
数据结构 i_love(我喜欢) 问题描述 集训队的学长们都怪怪的,如果 A 学长喜欢 B 学长, A 就会把自己的名字改成«I_love_<B 学长的名字>».但是奇怪的学长们很容易移情 ...
- springcloud zuulfilter 实现get,post请求日志记录功能
import com.alibaba.fastjson.JSONObject; import com.idoipo.infras.gateway.open.model.InvokeLogModel; ...
- C/C++使用心得:enum与int的相互转换
如何正确理解enum类型? 例如: enum Color { red, white, blue}; Color x; 我们应说x是Color类型的,而不应将x理解成enumeration类型,更不应将 ...