日志系列---【SpringBoot使用Aop实现格式化日志】
2021/11/16 6:40:59
本文主要是介绍日志系列---【SpringBoot使用Aop实现格式化日志】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.最终实现效果
2.在pom中引入Aop依赖(可以先写个@Aspect注解,如果不报错,说明项目中引入过aop依赖了,不用再重复引入下面的依赖)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
3.这里我项目中用了swagger,为了不重复写注释,我直接用了@ApiOperation注解作为切点,当然,也可以自定义注解,把ApiOperation换成自定义的就行了。
package com.system.annotation.Handler; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; @Slf4j @Aspect @Component public class LogHandler { @Pointcut("@annotation(io.swagger.annotations.ApiOperation)") public void webLog() { } @Around("webLog()") public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { long startTime = System.currentTimeMillis(); Object result = proceedingJoinPoint.proceed(); //打印出参 log.info("Response Args : {}", result); //执行耗时 log.info("exe-time : {} ms", System.currentTimeMillis() - startTime); log.info("============================================End==================================================="+System.lineSeparator()); return result; } @Before("webLog()") public void doBefore(JoinPoint joinPoint){ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); ApiOperation annotation = method.getAnnotation(ApiOperation.class); String msg = annotation.value(); log.info(System.lineSeparator()); log.info("============================================Start================================================="); //打印请求的url log.info("URL : {}",request.getRequestURL().toString()); log.info("Description : {}",msg); log.info("HTTP Method : {}",request.getMethod()); log.info("Class Method : {},{}",joinPoint.getSignature().getDeclaringTypeName(),joinPoint.getSignature().getName()); log.info("IP : {}",request.getRemoteAddr()); Object[] args = joinPoint.getArgs(); log.info("Request Args : {}",args); } @After("webLog()") public void doAfter() throws Throwable{ log.info("==========================================Response================================================="); } }
4.自定义注解(可以不自定义,我直接用的ApiOperation)
package com.jiulong.springboot_validator.annotation; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Documented public @interface WebLog { /** * 日志描述信息 * * @return String */ String value() default ""; }
这篇关于日志系列---【SpringBoot使用Aop实现格式化日志】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求