AOP和注解的配合使用(封装通用日志处理类)

2024-09-03 14:52

本文主要是介绍AOP和注解的配合使用(封装通用日志处理类),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

自定义注解

@Inherited
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {String value() default "";
}

定义切面

@Aspect
@Component
@Slf4j
public class LogAop {// 定义识别自定义注解的切点@Pointcut("@annotation(com.jxy.MyLog)")public void logger(){}/**** @param point* @return*/@Around("logger()")public Object around(ProceedingJoinPoint point) {Object obj = null;String ip_port = "";String uri = "";String value="";HttpServletResponse response = null;ControllerLogMapper bip2NCLogMapper = null;bip2NCLogMapper = AppContext.getBean(ControllerLogMapper.class);Signature methodName = point.getSignature();String name = point.getSignature().getName();log.error(methodName + "....running");//Long start = System.currentTimeMillis();Object[] argArray = point.getArgs();if (argArray != null && argArray.length > 0) {for (Object object : argArray) {if (object instanceof HttpServletResponse) {response = (HttpServletResponse) object;}}}// 目标方法执行try {Object res = point.proceed(point.getArgs());obj = res ;} catch (Throwable throwable) {obj = "【" + name + "方法异常,异常信息:" + throwable + "】";log.error("【环绕异常通知】【" + name + "方法异常,异常信息:" + throwable + "】");throw new GlobalException(throwable.getMessage());} finally {try {if (response != null) {PrintWriter writerToBeRead = response.getWriter();CoyoteWriter cw = (CoyoteWriter) writerToBeRead.append("");Class<?> clazz = cw.getClass();Field declaredField = clazz.getDeclaredField("ob");declaredField.setAccessible(true);OutputBuffer ob = (OutputBuffer) declaredField.get(cw);Class<?> classOutputBuffer = ob.getClass();//获取IP和端口 startClass<?> responseClass = response.getClass();Field requestField = responseClass.getDeclaredField("request");requestField.setAccessible(true);HttpServletRequest request = (HttpServletRequest) requestField.get(response);String ip = request.getRemoteAddr();//获取请求地址uri = request.getRequestURI();Field coyoteResponseField = classOutputBuffer.getDeclaredField("coyoteResponse");coyoteResponseField.setAccessible(true);Response coyoteResponse = (Response) coyoteResponseField.get(ob);Class<?> coyoteResponseClass = coyoteResponse.getClass();Field reqField = coyoteResponseClass.getDeclaredField("req");reqField.setAccessible(true);Request request1 = (Request) reqField.get(coyoteResponse);int port = request1.getServerPort();ip_port = ip + ":" + port;//获取IP和端口 endField fieldOutputChunk = classOutputBuffer.getDeclaredField("cb");fieldOutputChunk.setAccessible(true);obj = fieldOutputChunk.get(ob) == null ? "" : fieldOutputChunk.get(ob).toString();}MethodSignature signature = (MethodSignature) point.getSignature();MyLog declaredAnnotation = signature.getMethod().getDeclaredAnnotation(MyLog.class);value = declaredAnnotation.value();log.error("==注解@MyLog的value=="+ value);//记录到日志表String loginUser = AppContext.getCurrentUser() == null ? "第三方调用" : AppContext.getCurrentUser().getId().toString();String YTenantId = com.yonyou.eforship.common.utils.yms.AppContextUtil.getCurrentOrDefaultTenantId() == null ? "" : com.yonyou.eforship.common.utils.yms.AppContextUtil.getCurrentOrDefaultTenantId().toString();bip2NCLogMapper.add(String.valueOf(IdManager.getInstance().nextId()), loginUser, ArrayUtils.toString(argArray, ","), obj == null?"":obj.toString(), methodName.toString(), YTenantId, ip_port,uri,value);} catch (Exception e) {log.error("ControllerLog接口日志创建异常:" + e.getMessage());}log.error("【环绕后置通知】【" + name + "方法结束】");}//Long end = System.currentTimeMillis();//log.info(methodName + "....stop" + "\t耗时:" + (end - start));return obj;}public String getResponseContent(HttpServletResponse response) throws IOException, NoSuchFieldException, IllegalAccessException {String responseContent = null;CoyoteOutputStream outputStream = (CoyoteOutputStream) response.getOutputStream();Class<CoyoteOutputStream> coyoteOutputStreamClass = CoyoteOutputStream.class;Field obField = coyoteOutputStreamClass.getDeclaredField("ob");if (obField.getType().toString().endsWith("OutputBuffer")) {obField.setAccessible(true);org.apache.catalina.connector.OutputBuffer outputBuffer = (org.apache.catalina.connector.OutputBuffer) obField.get(outputStream);Class<org.apache.catalina.connector.OutputBuffer> opb = org.apache.catalina.connector.OutputBuffer.class;Field outputChunkField = opb.getDeclaredField("outputChunk");outputChunkField.setAccessible(true);if (outputChunkField.getType().toString().endsWith("ByteChunk")) {ByteChunk bc = (ByteChunk) outputChunkField.get(outputBuffer);Integer length = bc.getLength();if (length == 0) return null;responseContent = new String(bc.getBytes(), "UTF-8");Integer responseLength = StringUtils.isBlank(responseContent) ? 0 : responseContent.length();if (responseLength < length) {responseContent = responseContent.substring(0, responseLength);} else {responseContent = responseContent.substring(0, length);}}}return responseContent;}/*** 如果程序出现了异常,则需要拦截,打印异常信息* @param joinPoint* @param throwable*/@AfterThrowing(pointcut = "logger()",throwing = "throwable")public void afterThrow(JoinPoint joinPoint, Throwable throwable) {Class<?> targetClass = joinPoint.getTarget().getClass();String methodName = joinPoint.getSignature().getName();Class<?> throwClass = throwable.getClass();String msg = throwable.getMessage();log.error("目标对象类型:" + targetClass);log.error("目标方法:" + methodName);log.error("异常类型:" + throwClass);log.error("异常信息:" + msg);}}

使用

该自定义注解作用于方法上,如果需要日志处理,直接使用注解即可。

这篇关于AOP和注解的配合使用(封装通用日志处理类)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1133276

相关文章

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

Spring Boot整合log4j2日志配置的详细教程

《SpringBoot整合log4j2日志配置的详细教程》:本文主要介绍SpringBoot项目中整合Log4j2日志框架的步骤和配置,包括常用日志框架的比较、配置参数介绍、Log4j2配置详解... 目录前言一、常用日志框架二、配置参数介绍1. 日志级别2. 输出形式3. 日志格式3.1 PatternL

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Springboot 中使用Sentinel的详细步骤

《Springboot中使用Sentinel的详细步骤》文章介绍了如何在SpringBoot中使用Sentinel进行限流和熔断降级,首先添加依赖,配置Sentinel控制台地址,定义受保护的资源,... 目录步骤 1: 添加 Sentinel 依赖步骤 2: 配置 Sentinel步骤 3: 定义受保护的

Python中Markdown库的使用示例详解

《Python中Markdown库的使用示例详解》Markdown库是一个用于处理Markdown文本的Python工具,这篇文章主要为大家详细介绍了Markdown库的具体使用,感兴趣的... 目录一、背景二、什么是 Markdown 库三、如何安装这个库四、库函数使用方法1. markdown.mark

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多