Laughing
spring boot整合log4j2使用日志切面
05/29
本文最后更新于2024年03月18日,已超过185天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
在spring boot 整合log4j2中,我们在spring boot
中整合了log4j
,这篇文章,我们通过增加切面,实现自动记录日志。
增加切面依赖
<!-- 增加切面依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
增加切面
@Aspect
@Component
@Slf4j
public class LogAspect {
/**
* 定义切点
* 所有controller包下的public方法
*/
@Pointcut("execution(public * net.xiangcaowuyu.log4j.controller..*.*(..))")
public void autoLog(){};
/**
* 方法执行前
* @param joinPoint 切点
*/
@Before("autoLog()")
public void doBefore(JoinPoint joinPoint){
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert servletRequestAttributes != null;
HttpServletRequest request = servletRequestAttributes.getRequest();
log.info("method---"+request.getMethod());
}
}
版权属于:
香草物语
本文链接:
https://www.xiangcaowuyu.net/java/integrating-log4j2-with-spring-boot-to-use-log-aspect.html(转载时请注明本文出处及文章链接)
作品采用:
支持一下