TypechoJoeTheme

香草物语

统计
登录
用户名
密码
/
注册
用户名
邮箱
输入密码
确认密码

spring boot整合log4j2使用日志切面

Laughing博主
2021-05-29
/
2 评论
/
908 阅读
/
65 个字
/
百度已收录
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());
    }
}
Dubbo
朗读
赞(1)
赞赏
感谢您的支持,我会继续努力哒!
版权属于:

香草物语

本文链接:

https://www.xiangcaowuyu.net/java/integrating-log4j2-with-spring-boot-to-use-log-aspect.html(转载时请注明本文出处及文章链接)

评论 (2)
  1. 青云 久住
    Windows 10 · Google Chrome

    支持一下

    2021-06-08 回复
  2. Laughing 闲逛
    Windows 10 · Google Chrome

    2021-06-09 回复